Inserting data into a table from a Dynamic SQL script - sql

I am trying to run this script:
DECLARE #Client VARCHAR(50)
DECLARE #SQL VARCHAR(MAX)
DECLARE #DBReporting VARCHAR(500)
DECLARE #DBSignet VARCHAR(500)
DECLARE #databasename varchar(100)
SET #SQL = ''
DECLARE db_cursor CURSOR FOR
SELECT name
FROM sys.databases
WHERE name like '%reporting%'
AND NOT Name Like '%UAT%'
AND NOT Name Like '%Test%'
AND NOT Name Like '%Demo%'
AND NOT Name like '%staging%'
AND NOT Name like '%server%'
AND state_desc <> 'offline'
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #databasename
WHILE ##FETCH_STATUS = 0
BEGIN
SET #Client = REPLACE(REPLACE(#databasename, 'SourcingPlatform_', ''), '_Reporting', '')
SET #DBSignet = 'SourcingPlatform_' + #Client + '_Signet_Tradeflow'
SET #DBReporting = 'SourcingPlatform_' + #Client + '_Reporting'
SET #SQL = #SQL + 'INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
([Project]
,[DataDate]
,[EventTypeName]
,[CountOfAllEvents]
,[CreatedWithinLast3Months]
,[CreatedWithinLast6Months]
,[CreatedWithinLast12Months])
VALUES
SELECT ''' + #Client + ''' AS Client, convert(date, getdate()), EventTypeName collate Latin1_General_CI_AS,
count(id) as CountOfAllEvents,
(select COUNT(e3.ID)
from ' + #DBReporting + '..REPORTS_Sourcing_Event E3
where DATEDIFF(month,CreateDate, GETDATE()) <= 3
and E.EventTypeName = E3.EventTypeName) as CreatedLast3Months,
(select COUNT(e6.ID)
from ' + #DBReporting + '..REPORTS_Sourcing_Event E6
where DATEDIFF(month,CreateDate, GETDATE()) > 3
and DATEDIFF(month,CreateDate, GETDATE()) <= 6
and E.EventTypeName = E6.EventTypeName) as CreatedLast6Months,
(select COUNT(e12.ID)
from ' + #DBReporting + '..REPORTS_Sourcing_Event E12
where DATEDIFF(month,CreateDate, GETDATE()) > 6
and DATEDIFF(month,CreateDate, GETDATE()) <= 12
and E.EventTypeName = E12.EventTypeName) as CreatedLast12Months,
(select COUNT(e13.ID)
from ' + #DBReporting + '..REPORTS_Sourcing_Event E13
where DATEDIFF(month,CreateDate, GETDATE()) > 12
and E.EventTypeName = E13.EventTypeName) as CreatedOver12Months
FROM ' + #DBReporting + '..REPORTS_Sourcing_Event E
Group By EventTypeName
UNION '
FETCH NEXT FROM db_cursor INTO #databasename
END
CLOSE db_cursor
DEALLOCATE db_cursor
SET #sql = substring(#sql, 0, LEN(#sql) - len('UNION ')) + ' ORDER BY Client, EventTypeName collate Latin1_General_CI_AS'
--PRINT #SQL
exec(#SQL)
However, I am getting a syntax error.
I have printed the #SQL variable and the code generated looks good to me. Am I missing something really simple here? or am I way off what I want to achieve?
What I want to achieve is a script that goes through each DB referenced in the first select and get the values and insert them into my table.
Let me know if you need anymore information to help me, any help at all at this point would be greatly appreciated.

You should post the generated query, but I think it looks something like:
INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
([Project]
,[DataDate]
,[EventTypeName]
,[CountOfAllEvents]
,[CreatedWithinLast3Months]
,[CreatedWithinLast6Months]
,[CreatedWithinLast12Months])
VALUES -- Remove this, it's incorrect in combination with SELECT
SELECT (lots of selects)
UNION
INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
([Project]
,[DataDate]
,[EventTypeName]
,[CountOfAllEvents]
,[CreatedWithinLast3Months]
,[CreatedWithinLast6Months]
,[CreatedWithinLast12Months])
SELECT (lots of selects)
This is obviously not possible, you want to union the selects not the insert. So you should begin by initialling #SQL with the insert statement (outside the cursor). Inside the cursor you can use SET #SQL = #SQL + ... as you are already doing, but without the insert statement.
Also, please note substring is 1 based in SQL, not 0 as in, for example, C#.

Related

SQL - How do I get 1 empty column per row in a related table?

I am trying to write a SQL query that adds a certain amount of empty columns, based on the number of rows in a related table (t1) for a Crystal Report. These columns should have the header of the name of the dataset.
So it should look something like this:
However I would need to change the script each time a new row gets added (e.g. opening a store - not very often, but it does happen).
I thought about using the pivot function, but I believe the number of rows must be defined - plus, there is no calculation / aggregation happening.
Does anybody have an idea on how to solve this?
As Larnu already mentioned, dynamic SQL would be one way to go. I would suggest using a combination of XML PATH and dynamic SQL. Following an example:
DECLARE #colList VARCHAR(MAX) = (SELECT STUFF((SELECT ',NULL as t1_row' + cast(col1 AS varchar(3))
FROM MyTable
FOR XML PATH('')) ,1,1,'') AS Txt
)
DECLARE #stmt VARCHAR(MAX) = 'SELECT Col1, Col2, Col3, ' + #colList + ' FROM MyTable'
EXEC (#stmt)
I was able to achieve the result using dynamic SQL.
The Script looks something like this:
DECLARE #STRSQL NVARCHAR(MAX) = 'WITH a AS (SELECT ';
DECLARE #Kst nvarchar(6);
DECLARE #Markt NVARCHAR(30);
DECLARE #SCHEMA_NAME VARCHAR(50) = 'XTRADE';
DECLARE C1 CURSOR FOR
SELECT NUMMER, BEZEICHNUNG
from XTRADE.KUNDE
where NUMMER > 99 and NUMMER not in (194, 196, 198)
and (DATUM_SCHLIESSUNG > GETDATE() or DATUM_SCHLIESSUNG is null)
order by BEZEICHNUNG
OPEN C1
PRINT #Kst + ' ' + #Markt
FETCH NEXT
FROM C1 into #Kst, #Markt
while ##FETCH_STATUS = 0
BEGIN
SET #STRSQL = #STRSQL + 'null as [' + #Markt + '], '
FETCH NEXT
FROM C1 into #Kst, #Markt
END
CLOSE C1
DEALLOCATE C1;
SET #STRSQL = left(#STRSQL, len(#Strsql) - 1) + ')'
DECLARE #Statement nvarchar(max) = ', b as (select 1 as Col1, 1 as Col2, 5 as Col3 union all select 2,2,12 union all select 3, 3, 42)';
DECLARE #Exec nvarchar(max) = #STRSQL + #Statement + 'select * from b cross join a';
print #Exec;
exec sp_executesql #Exec

How to Loop table creation

I have the following code which works fine for one table but I am trying to figure out how to make it loop.
DECLARE #sql NVARCHAR(2000)
DECLARE #table VARCHAR(100)
DECLARE #command NVARCHAR(1000)
SELECT #table = ( VV.SRC_CD )
FROM VV
SET #sql = 'SELECT [CD],[SRC_CD],[SRC_CD_DESC],[DSC],[REFRESH_DT],[GEN_DSC] = CAST(null as VARCHAR(50)) INTO dbo.vv_' + #table
+
' FROM [vv] A WHERE A.SRC_CD <> ''GEN'' AND A.DSC <> ''NO DATA'' AND A.DSC <> ''(BLANK) NO'' AND A.src_cd = '''
+ #table + ''''
EXEC Sp_executesql
#stmt = #sql
The code that populates the #table variable returns one row. What I am trying to do is get this procedure to loop one time for each distinct value in the table for that row. The distinct clause doesn't work here and I have tried a basic table array with no luck.
Do I need to modify this to use a cursor?
It sounds like you're in need of a cursor, something like:
DECLARE #Iterator NVARCHAR(100)
,#sql NVARCHAR(MAX)
DECLARE xyz CURSOR
FOR
--Select stuff to iterate over
SELECT DISTINCT SRC_CD
FROM w
OPEN xyz
FETCH NEXT FROM xyz
INTO #Iterator
WHILE ##FETCH_STATUS = 0
BEGIN
--Do stuff
SET #sql = 'SELECT [CD],[SRC_CD],[SRC_CD_DESC],[DSC],[REFRESH_DT],[GEN_DSC] = CAST(null as VARCHAR(50))
INTO dbo.vv_' + #Iterator +'
FROM [vv] A
WHERE A.SRC_CD <> ''GEN''
AND A.DSC <> ''NO DATA''
AND A.DSC <> ''(BLANK) NO''
AND A.src_cd = ''' + #Iterator + ''''
PRINT (#sql)
FETCH NEXT FROM xyz
INTO #Iterator
END
CLOSE xyz
DEALLOCATE xyz
GO
I left PRINT in there so you can ensure the output is as desired before executing.

How to avoid the cursor and what is the other way?

I am using the cursor in my stored procedure; I am hoping to remove the cursor from my SP. Please help me come up with a solution for how to avoid the cursor statement to normal update statement with dynamic.
Example Below:
Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'Test'(variable) and age = 18(variable)
Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'kumar'(variable) and age = 19(variable)
Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'babu'(variable) and age = 30(variable)
This is how my cursor will work. 300 Combination dynamically pick the data from table and update into the main table
I am trying to take out the cursor, and update statement should work similar to this, instead of writing 300 update statements, I want to write one update where all the 300 combinations should execute.
Below is my code which needs this solution:
BEGIN
DECLARE #Type VARCHAR(100)
DECLARE #TargetColumn VARCHAR(100)
DECLARE #SourceColumn VARCHAR(100)
DECLARE #SQL varchar(max)
DECLARE a_cursor CURSOR STATIC
FOR
SELECT [Type],[SourceColumn],[TargetColumn] FROM ref.tblEdsMap
GROUP BY [Type],[SourceColumn],[TargetColumn]
OPEN a_cursor
FETCH NEXT FROM a_cursor INTO #Type,#SourceColumn,#TargetColumn
WHILE ##FETCH_STATUS = 0
BEGIN
SET #SQL = 'UPDATE GCT SET GCT.' + #TargetColumn + ' = map.[TargetValue]
from EdsMap map
JOIN Table GCT
ON GCT.' + #SourceColumn + ' = map.[SourceValue]
where map.[Type]=''' + #Type + ''' and map.SourceColumn=''' + #SourceColumn+ ''''
Exec (#SQL)
PRINT #SQL
FETCH NEXT FROM a_cursor INTO #Type,#SourceColumn,#TargetColumn
END
CLOSE a_cursor
DEALLOCATE a_cursor
END
Rather than use an explicit cursor or a cursor cleverly disguised as a while loop, I prefer row concatenation operations for this type of problem.
DECLARE #cmd NVARCHAR(MAX) = N'';
SELECT #cmd += N'
UPDATE GCT
SET GCT.' + QUOTENAME(TargetColumn) + ' = map.TargetValue
FROM dbo.EdsMap AS map
INNER JOIN dbo.Table AS GCT
ON GCT.' + QUOTENAME(SourceColumn) + ' = map.SourceValue
WHERE map.[Type] = ''' + [Type] + '''
AND map.SourceColumn = ''' + [SourceColumn]+ ''';'
FROM ref.tblEdsMap
GROUP BY [Type], SourceColumn, TargetColumn;
EXEC sp_executesql #sql;
When I've done these in the past, I usually make up a transaction to encompass every update that's needed. Something like this:
CREATE TABLE #targets ([Type] VARCHAR(255),[SourceColumn] VARCHAR(255),[TargetColumn] VARCHAR(255));
INSERT INTO #targets
( [Type], [SourceColumn], [TargetColumn] )
SELECT [Type],[SourceColumn],[TargetColumn] FROM ref.tblEdsMap
GROUP BY [Type],[SourceColumn],[TargetColumn];
DECLARE #sql VARCHAR(MAX);
SET #sql = 'BEGIN TRAN' + CHAR(10) + CHAR(13);
SELECT #sql = #sql +
'UPDATE GCT SET GCT.' + [TargetColumn] + ' = map.[TargetValue]
from EdsMap map
JOIN Table GCT
ON GCT.' + [SourceColumn] + ' = map.[SourceValue]
where map.[Type]=''' + [Type] + ''' and map.SourceColumn=''' + [SourceColumn]+ ''';' + CHAR(10) + CHAR(13)
FROM #targets
SELECT #sql = #sql + 'COMMIT TRAN'
PRINT #sql
Exec (#SQL)
The update statements are still the same, i.e., you get one update per combination. But now you're running as one transaction batch. You could potentially be fancier with the dynamic SQL, so that you had just one update statement, but in my experience, it's too easy to get bad updates that way.
Doing it this way may not be any faster than a cursor. You'd have to test to be sure. With the examples where I've used this approach, it has generally been a faster approach.
Try using a table variable along with a WHILE loop instead, like so:
BEGIN
DECLARE #Type VARCHAR(100)
DECLARE #TargetColumn VARCHAR(100)
DECLARE #SourceColumn VARCHAR(100)
DECLARE #SQL varchar(max)
DECLARE #SomeTable TABLE
(
ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
Type varchar(100),
SourceColumn varchar(100),
TargetColumn varchar(100)
)
DECLARE #Count int, #Max int
INSERT INTO #SomeTable (Type, SourceColumn, TargetColumn)
SELECT [Type],[SourceColumn],[TargetColumn]
FROM ref.tblEdsMap
GROUP BY [Type],[SourceColumn],[TargetColumn]
SELECT #Count = 1, #Max = COUNT(ID)
FROM #SomeTable
WHILE #Count <= #Max
BEGIN
SELECT
#Type = Type,
#SourceColumn = SourceColumn,
#TargetColumn = TargetColumn
FROM #SomeTable
WHERE ID = #Count
-- Your code
SET #SQL = 'UPDATE GCT SET GCT.' + #TargetColumn + ' = map.[TargetValue]
from EdsMap map
JOIN Table GCT
ON GCT.' + #SourceColumn + ' = map.[SourceValue]
where map.[Type]=''' + #Type + ''' and map.SourceColumn=''' + #SourceColumn+ ''''
Exec (#SQL)
PRINT #SQL
SET #Count = #Count + 1
END -- while
END

patch datetime in varchar column

i have about 12 tables i need to iterate thru and update all datetime fields (which is a varchar type) check if it's a date and if so, add 5hrs to account for utc adjustment or if it's an invalid date just set it to null. Below is a template I came up with. Just wondering if there are some other ways to do this?
update Tables_1
set releasedate = CASE ISDATE(releasedate)
WHEN 1 THEN DATEADD(HH, 5, releasedate)
ELSE NULL
END,
returndate = CASE ISDATE(returndate)
WHEN 1 THEN DATEADD(HH, 5, returndate)
ELSE NULL
END
given: sql server 2008, i know the columns and tables already that have the datetime type stored as varchar.
bonus request to add another check. If it is a date and the time is not specified, set the time to 5:00 AM
If they all end with date you could build it dynamically:
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql += ', ' + name = CASE ISDATE(' + name + ') WHEN 1 THEN
DATEADD(HOUR, 5, ' + name + ' ELSE NULL END'
FROM sys.columns
WHERE name LIKE '%date' AND [object_id] = OBJECT_ID('dbo.Tables_1');
SELECT #sql = 'UPDATE dbo.Tables_1 SET ' + STUFF(#sql, 1, 1, N'');
PRINT #sql;
-- EXEC sp_executesql #sql;
Now if you want to do that for 12 tables, you could just do this in a loop, e.g.
DECLARE #t SYSNAME, #sql NVARCHAR(MAX);
DECLARE c CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
FOR
SELECT name FROM sys.tables
WHERE name IN ('Tables_1' --, other tables
);
OPEN c;
FETCH NEXT FROM c INTO #t;
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #sql = ', ' + name = CASE ISDATE(' + name + ') WHEN 1 THEN
DATEADD(HOUR, 5, ' + name + ' ELSE NULL END'
FROM sys.columns
WHERE name LIKE '%date' AND [object_id] = OBJECT_ID(#t);
SELECT #sql = 'UPDATE ' + #t + ' SET ' + STUFF(#sql, 1, 1, N'');
PRINT #sql;
-- EXEC sp_executesql #sql;
FETCH NEXT FROM c INTO #t;
END
CLOSE c;
DEALLOCATE c;

Execute a Stored Procedure in a SELECT statement

For an instance I a select statement and it is returning 1000 rows. I need to execute a particular stored procedure for every row the the select statement is returning.
have you got any idea how can I do that?
Construct the EXECUTE statements in your select like this:
SELECT 'EXEC sp_whatever ' + parameter stuff
FROM your_table
Then run the results! Alternatively, paste your results into a spreadsheet package, and use string concatenation to construct the EXEC statements - just create a formula and paste it down the 1,000 rows. I personally prefer the first approach.
To clarify the "parameter stuff", take the example of a stored procedure that takes two int parameters that you want to take from columns you your_table. You'd then have something like this:
SELECT 'EXEC sp_whatever ' + CAST(field1 AS varchar) + ', ' + CAST(field2 AS varchar)
FROM your_table
Not the need to be careful with string fields here - you run the risk of inadvertently exposing yourself to your own SQL injection attack, as with any SQL string concatenation.
I am reading your "for an instance" as "this is a one-off task". If this is a task that needs automating, then one of the other answers may be the right approach.
You can do it like this:
declare #execstatementsbatch nvarchar(max)
select #execstatementsbatch = ''
SELECT #execstatementsbatch = #execstatementsbatch + 'EXEC UpdateQty ' + ItemCode + ', ' + QtyBO + '; '
FROM ITEMSPO
INNER JOIN .....
<some conditions>
exec(#execstatementsbatch)
Disclaimer: I'm not sure if I understand your question correctly.
Assuming you are on SQL Server 2005 upwards, you could create a table-valued user defined function and use the OUTER APPLY operator in your query.
Most RDBMS will let you select rows from stored procedure result sets. Just put your stored procedures in the FROM clause, as you would for common table expressions. For instance:
SELECT sp.ColumnInResultSet, t.BaseTableColumnName
FROM sp_whatever ( Args) sp INNER JOIN BaseTable t ON t.ID = sp.ID;
CREATE PROCEDURE dbo.usp_userwise_columns_value
(
#userid BIGINT
)
AS
BEGIN
DECLARE #maincmd NVARCHAR(max);
DECLARE #columnlist NVARCHAR(max);
DECLARE #columnname VARCHAR(150);
DECLARE #nickname VARCHAR(50);
SET #maincmd = '';
SET #columnname = '';
SET #columnlist = '';
SET #nickname = '';
DECLARE CUR_COLUMNLIST CURSOR FAST_FORWARD
FOR
SELECT columnname , nickname
FROM dbo.v_userwise_columns
WHERE userid = #userid
OPEN CUR_COLUMNLIST
IF ##ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
FETCH NEXT FROM CUR_COLUMNLIST
INTO #columnname, #nickname
WHILE ##FETCH_STATUS = 0
BEGIN
SET #columnlist = #columnlist + #columnname + ','
FETCH NEXT FROM CUR_COLUMNLIST
INTO #columnname, #nickname
END
CLOSE CUR_COLUMNLIST
DEALLOCATE CUR_COLUMNLIST
IF NOT EXISTS (SELECT * FROM sys.views WHERE name = 'v_userwise_columns_value')
BEGIN
SET #maincmd = 'CREATE VIEW dbo.v_userwise_columns_value AS SELECT sjoid, CONVERT(BIGINT, ' + CONVERT(VARCHAR(10), #userid) + ') as userid , '
+ CHAR(39) + #nickname + CHAR(39) + ' as nickname, '
+ #columnlist + ' compcode FROM dbo.SJOTran '
END
ELSE
BEGIN
SET #maincmd = 'ALTER VIEW dbo.v_userwise_columns_value AS SELECT sjoid, CONVERT(BIGINT, ' + CONVERT(VARCHAR(10), #userid) + ') as userid , '
+ CHAR(39) + #nickname + CHAR(39) + ' as nickname, '
+ #columnlist + ' compcode FROM dbo.SJOTran '
END
--PRINT #maincmd
EXECUTE sp_executesql #maincmd
END
-----------------------------------------------
SELECT * FROM dbo.v_userwise_columns_value