Hi @SQL9 ,
Thank you for asking this question. Try the following statements.
Hope this helps. If it does, please don't forget to "accept the answer" and "up-vote"! Thank you!
create table SQLData
(
ID int null , TableName varchar(100) null , SQLText Varchar(max) null
)
insert into SQLData (ID , TableName , SQLText)
Values
(1 , '[dbo].Customer' , concat((Select count(customeraddress1) from [dbo].Customer where customeraddress1 is not null), ' Select count(customeraddress1) from [dbo].Customer where customeraddress1 is not null')),
(2 , '[dbo].Customer' , concat((Select count(customeraddress2) from [dbo].Customer where customeraddress2 is not null), ' Select count(customeraddress2) from [dbo].Customer where customeraddress2 is not null')),
(3 , '[dbo].Customer' , concat((Select count(customeraddress3) from [dbo].Customer where customeraddress3 is not null), ' Select count(customeraddress3) from [dbo].Customer where customeraddress3 is not null')),
(4 , '[dbo].Product' , concat((Select count(ProductDescription) from [dbo].Product where ProductDescription is not null), ' Select count(ProductDescription) from [dbo].Product where ProductDescription is not null')),
(5 , '[dbo].Product' , concat((Select count(Discount) from [dbo].Product where Discount is not null), ' Select count(Discount) from [dbo].Product where Discount is not null'))
Select * from SQLData
I purposefully avoided producing a single statement in my solution.
We don't know how many rows there are the actual table, but the VALUES constructor has a limit of 1000 rows. This can be avoided by using SELECT with UNION ALL instead. However, there is a high cost for compiling statements with many UNION operators, and this is visible already with less than 1000 of them. (The limitation with VALUES exists because exactly of this reason. VALUES is only syntactic sugar on SELECT UNION ALL.)
For that reason, my solution produces one SQL statement per row, and then I collect all rows with INSERT-EXEC.
any updates?
Echo