sql - View using dynamic tables
问题描述
How can i convert the following query into something that i can create a view with.
create view hourlyBilling_view as
declare @sql varchar(max)
set @sql = ''
select @sql = @sql + case len(@sql) when 0 then '' else ' UNION ALL ' end + '
SELECT * FROM [' + table_name + '] where line_item_6 is not null '
from
information_schema.tables where table_name like 'Billing_trap_2019%'
exec (@sql)
It give me an error about the declare can not be used.
I have checked online and see it might be possible, but I don't have the SQL skills to figure it out.
解决方案
不能在视图中使用动态 sql,甚至不能在表值函数中使用。最接近您想要的是存储过程。
推荐阅读
- javascript - 在打字稿中实现安全导航
- css - 在 div 中水平居中文本
- java - 滚动项目时,Android ListView Adapter 会改变颜色
- plsql - 将小数作为 SQL 函数参数传递时出现数值或估值错误
- selenium - OWASP ZAP - 使用 Selenium Python 启动浏览器后提取 URL
- php - whatsapp api在php密码创建错误中发送消息
- c++ - C++ 中的方括号和对类型
- c++ - 三参数函数模板混淆示例
- bash - 如何使用 sed 用空格代替 LF,而不是 CRLF?
- python - 如何在线程中使用 QDateTime?