动态 SQL 创建视图
Dynamic SQL To Create View
我有一个 table 列表,里面列出了表现不佳的商店名称。每个商店名称在服务器上都有自己的数据库(我们正在努力合并所有数据库,但目前都是分开的)。是否可以迭代 table(称为 failingstores)并创建一个视图来保存所有失败商店的数据?这是table结构:(当然是垃圾数据)
create table failingstores
(
storename varchar(100)
,weeklysales int
,monthlysales int
,storemanager varchar(100)
)
Insert Into failingstores Values
('one', 80, 800, 'managerone'),
('two', 90, 900, 'managertwo'),
('three', 40, 400, 'managerthree'),
('four', 10, 100, 'managerfour')
我希望发生的动态 sql 是迭代 storename 字段中的值并创建一个视图。所以这将是我的视图语法:
Create view failingstoresfulldata As
Select * from managerone
union all
select * from managertwo
union all
select * from managerthree
union all
select * from managerfour
我最近不得不做一些类似的事情,并使用动态 SQL 将数据从数据库提取到本地 table。存储到本地 table 当然是可选的,但是一旦它在那里,您就可以在上面构建任何其他 SQL 对象来查询您的心满意足!
IF OBJECT_ID(N'tempdb..#Databases') is not null
Drop Table #Databases
Select Name
Into #Databases
From (Values
('Company1DB')
,('Company2DB')
,('Company3DB')
) as db(Name)
Declare @DBName varchar(50),@SQL nvarchar(max)=''
While (select count(*) From #Databases) >1 Begin
Select Top 1 @DBName=Name From #Databases
Select @DBName=isnull(@DBName,'')
Set @SQL=@SQL+'Union All
SELECT
Column1
,Column2
,Column3
FROM '+@DBName+'.tablename'
Delete From #Databases where Name=@DBName
End
Select @SQL=Stuff(@SQL,1,9,'') --- Remove leading "Union All"
Select @SQL --- Preview of Query
--- Pull the data into a single table for easy querying or further manipulation ---
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'FullData') AND type in (N'U')) Begin
Create Table FullData (
Column1 int
,Column2 int
,Column3 int
)
End Else Begin
Truncate Table FullData
End
Insert Into FullData Exec SP_ExecuteSQL @SQL,N''
--- Cleanup ---
Drop Table #Databases
我更喜欢更简单的解决方案:
declare @sql varchar(max) = 'Create view failingstoresfulldata As
'
select @sql = @sql +
'Select * from ' + storemanager + '
union all
'
from failingstores
set @sql = left(@sql, len(@sql)-11)
print @sql
--exec (@sql)
首先用 print
检查生成的动态 SQL,然后取消注释 exec
命令以 运行 它下一次。
我有一个 table 列表,里面列出了表现不佳的商店名称。每个商店名称在服务器上都有自己的数据库(我们正在努力合并所有数据库,但目前都是分开的)。是否可以迭代 table(称为 failingstores)并创建一个视图来保存所有失败商店的数据?这是table结构:(当然是垃圾数据)
create table failingstores
(
storename varchar(100)
,weeklysales int
,monthlysales int
,storemanager varchar(100)
)
Insert Into failingstores Values
('one', 80, 800, 'managerone'),
('two', 90, 900, 'managertwo'),
('three', 40, 400, 'managerthree'),
('four', 10, 100, 'managerfour')
我希望发生的动态 sql 是迭代 storename 字段中的值并创建一个视图。所以这将是我的视图语法:
Create view failingstoresfulldata As
Select * from managerone
union all
select * from managertwo
union all
select * from managerthree
union all
select * from managerfour
我最近不得不做一些类似的事情,并使用动态 SQL 将数据从数据库提取到本地 table。存储到本地 table 当然是可选的,但是一旦它在那里,您就可以在上面构建任何其他 SQL 对象来查询您的心满意足!
IF OBJECT_ID(N'tempdb..#Databases') is not null
Drop Table #Databases
Select Name
Into #Databases
From (Values
('Company1DB')
,('Company2DB')
,('Company3DB')
) as db(Name)
Declare @DBName varchar(50),@SQL nvarchar(max)=''
While (select count(*) From #Databases) >1 Begin
Select Top 1 @DBName=Name From #Databases
Select @DBName=isnull(@DBName,'')
Set @SQL=@SQL+'Union All
SELECT
Column1
,Column2
,Column3
FROM '+@DBName+'.tablename'
Delete From #Databases where Name=@DBName
End
Select @SQL=Stuff(@SQL,1,9,'') --- Remove leading "Union All"
Select @SQL --- Preview of Query
--- Pull the data into a single table for easy querying or further manipulation ---
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'FullData') AND type in (N'U')) Begin
Create Table FullData (
Column1 int
,Column2 int
,Column3 int
)
End Else Begin
Truncate Table FullData
End
Insert Into FullData Exec SP_ExecuteSQL @SQL,N''
--- Cleanup ---
Drop Table #Databases
我更喜欢更简单的解决方案:
declare @sql varchar(max) = 'Create view failingstoresfulldata As
'
select @sql = @sql +
'Select * from ' + storemanager + '
union all
'
from failingstores
set @sql = left(@sql, len(@sql)-11)
print @sql
--exec (@sql)
首先用 print
检查生成的动态 SQL,然后取消注释 exec
命令以 运行 它下一次。