0% found this document useful (0 votes)
43 views1 page

Query

This document contains a SQL script that: 1) Declares two tables - one to store table names from the database and one to store a batch ID. 2) Populates the table name table by querying the database for all table names. 3) Declares variables to track the count and total number of tables. 4) Loops through each table name, builds an update statement to set the BatchID where the ContractID has been in effect for over 15 months, prints the statement for checking, and then executes the dynamic SQL.

Uploaded by

gobeyondsky1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views1 page

Query

This document contains a SQL script that: 1) Declares two tables - one to store table names from the database and one to store a batch ID. 2) Populates the table name table by querying the database for all table names. 3) Declares variables to track the count and total number of tables. 4) Loops through each table name, builds an update statement to set the BatchID where the ContractID has been in effect for over 15 months, prints the statement for checking, and then executes the dynamic SQL.

Uploaded by

gobeyondsky1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

declare @tabs table(i int identity,TabName nvarchar(100)) insert into @tabs(TabName) select table_name from information_schema.

tables(or any source which has the tables) declare @cnt int,@RCnt int,@tabtar nvarchar(100),@sql nvarchar(200) Set @Cnt=1 Set @Rcnt=(Select COUNT(*) from @tabs) --print @Rcnt --print @Cnt --select * from @tabs While(@RCnt>=@cnt) Begin Select @tabtar=TabName from @tabs where i=@cnt set @sql='UPDATE '+@tabtar+' SET BatchID=BatchID WHERE ContractID
IN(SELECT * FROM abctable WHERE DATEDIFF("mm",DataEffDate,GETDATE())>=15)'

print @sql--To check ur Stmt exec(@sql) set @cnt=@cnt+1 End

You might also like