我已经设法从一个游标创建一个表,该游标从我的数据库中类似命名的表中插入行。它成功运行,但我想创建一个视图,这样我就不需要更新我的原始查询。我可能会使用游标走错路线,但我当前的查询是:
use LaganPDM
set nocount on
declare @table varchar(128)
declare @cmd varchar(500)
create table SPECIAL_CASE_FORMS_2 (table_name varchar(128), flods_id numeric(22,0) PRIMARY KEY, lgncc_id numeric(22,0), case_enquiry_id numeric(22,0),amount varchar(4000), costcode varchar(4000), jobcompletedate varchar(4000), jobreleasedate varchar(4000),paymentstatus varchar(4000))
declare tables cursor for
select table_name
from information_schema.tables
where table_name like '%SPECIAL_UP_C00%'
and left(right(table_name, 24),9) > '101000363'
and not left(right(table_name, 24),9) in('101000487', '101000507', '101000510')
open tables
fetch next from tables into @table
while @@fetch_status = 0
begin
set @cmd = 'select ''' + @table + ''', * from ' + @table
insert into SPECIAL_CASE_FORMS_2 exec (@cmd)
fetch next from tables into @table
END
CLOSE tables
DEALLOCATE tables
select * from SPECIAL_CASE_FORMS_2
我试图适应这一点并创建一个视图,但没有运气。任何建议都将不胜感激,即使这意味着走不同的路线。
谢谢!