Dynamic SQL string returns NULL - sql-server

I am trying to construct a dynamic SQL statement and for some reason it is not returning the expected results.
This is my query
DECLARE #user_script AS VARCHAR(MAX);
SELECT #user_script += 'IF NOT EXISTS (SELECT [name] FROM sys.database_principals WHERE [name] = '
+ SPACE(1) + '''' + [name] + '''' + ') BEGIN CREATE USER ' + SPACE(1)
+ QUOTENAME([name]) + ' FOR LOGIN ' + QUOTENAME([name])
+ ' WITH DEFAULT_SCHEMA = ' + QUOTENAME([default_schema_name])
+ SPACE(1) + 'END; '
FROM sys.database_principals AS rm
WHERE [type] IN ( 'U', 'S', 'G' )
AND rm.default_schema_name IS NOT NULL;
PRINT ( #user_script );
My problem is that the print statement is not returning anything which I think is because the #user_script variable is NULL.
To evaluate if my table is actually returning results I ran this query (exact copy of the above query without assigning it to a variable) and this query returns 10 rows
SELECT 'IF NOT EXISTS (SELECT [name] FROM sys.database_principals WHERE [name] = '
+ SPACE(1) + '''' + [name] + '''' + ') BEGIN CREATE USER ' + SPACE(1)
+ QUOTENAME([name]) + ' FOR LOGIN ' + QUOTENAME([name])
+ ' WITH DEFAULT_SCHEMA = ' + QUOTENAME([default_schema_name])
+ SPACE(1) + 'END; '
FROM sys.database_principals AS rm
WHERE [type] IN ( 'U', 'S', 'G' )
AND rm.default_schema_name IS NOT NULL;
I tried replacing the function QUOTENAME() with quotes and SPCACE() with actual space but I got the same issue.
I finally checked to see if any of the returned results from the second query is NULL. But none of the rows was a NULL.
Thanks for your help

Try putting a
SET #user_script = ''
on the line before your
select #user_script += ....

You can start the select with
SELECT #user_script = ISNULL(#user_script, '') + ...

Related

Using sub-query to generate case statements

What i am trying to accomplish is comparing two rows to each other pointing out the differences from row to row. Each row has quite a few columns and I was trying to make it easily visible for which ones had changed. Code below is my thoughts, but I know this won't work, but is a start.
SELECT
(SELECT concat('Case WHEN T1.', column_name, ' <> T2.', column_name, ' THEN ''', column_name, ' Changed Values('' + CONVERT(varchar(100), T1.', column_name, ') + '', '' + CONVERT(varchar(100), T2.', column_name, ') + '')'' ELSE '''' END AS ', column_name)
FROM information_schema.columns
WHERE table_name = 'Table')
FROM
(
SELECT * FROM Table
WHERE ID = '13'
) AS T1
JOIN
(
SELECT * FROM Table
WHERE ID = '2006'
) AS T2
ON T1.CreateTimeStamp = T2.CreateTimeStamp
I got the idea because below this works fine, but I would like this to be potentially reusable code for other table without having to type out tens or hundreds of columns each time.
SELECT
Case WHEN T1.R1<> T2.R1 THEN 'Changed Values(' + CONVERT(varchar(100),T1.R1) + ', ' + CONVERT(varchar(100),T2.R1) + ')' ELSE '' END AS R1,
Case WHEN T1.R2<> T2.R2 THEN 'Changed Values(' + CONVERT(varchar(100),T1.R2) + ', ' + CONVERT(varchar(100),T2.R2) + ')' ELSE '' END AS R2
FROM
(
SELECT * FROM Table
WHERE ID = '13'
) AS T1
JOIN
(
SELECT * FROM Table
WHERE ID = '2006'
) AS T2
ON T1.CreateTimeStamp = T2.CreateTimeStamp
For the this example please assume CreateTimeStamp always equals each other between the two rows.
You would need to create the whole query as dynamic SQL. Note that I'm using QUOTENAME() to prevent SQL Injection from weirdly named columns. I'm also trying to keep a format for the code, so I won't get headaches when debugging.
DECLARE #SQL NVARCHAR(MAX);
SELECT #SQL = N' SELECT ' + NCHAR(10)
--Concatenate all columns except ID and CreateTimeStamp
+ STUFF(( SELECT REPLACE( CHAR(9) + ',CASE WHEN T1.<<ColumnName>> <> T2.<<ColumnName>> ' + CHAR(10)
+ CHAR(9) + CHAR(9) + 'THEN ''Changed Values('' + CONVERT(varchar(100),T1.<<ColumnName>>) + '', '' + CONVERT(varchar(100),T2.<<ColumnName>>) + '')'' ' + CHAR(10)
+ CHAR(9) + CHAR(9) + 'ELSE '''' END AS <<ColumnName>>', '<<ColumnName>>', QUOTENAME(COLUMN_NAME)) + NCHAR(10)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table'
AND COLUMN_NAME NOT IN( 'ID', 'CreateTimeStamp')
FOR XML PATH(''), TYPE).value('./text()[1]', 'nvarchar(max)'), 2, 1, '') + NCHAR(10)
--Add rest of the query
+ 'FROM Table AS T1 ' + NCHAR(10)
+ 'JOIN Table AS T2 ON T1.CreateTimeStamp = T2.CreateTimeStamp ' + NCHAR(10)
+ 'WHERE ID = #ID1 ' + NCHAR(10)
+ 'AND ID = #ID2;'
--PRINT for debugging purposes
PRINT #SQL;
--Execute the dynamic built code
EXECUTE sp_executesql #SQL,
N'#ID1 int, #ID2 int',
#ID1 = 13,
#ID2 = 2006;
The concatenation method is explained on this article.

SQL dynamic query check for null, or empty string parameter

I am writing a stored procedure that will be executed from an SSRS report. It receives 2 parameters: SourceID, and ConfirmationNumber but it is not working for all the tests that I am running
this returns recs:[dbo].[GetParkingPaymentInformation] 'PARC', ''
this does not return recs:[dbo].[GetParkingtPaymentInformation] 'PARC', NULL
this does not return recs:[dbo].[GetParkingPaymentInformation] '', '002077770'
this does not return recs:[dbo].[GetParkingPaymentInformation] NULL, '002077770'
this does return recs:[dbo].[GetParkingPaymentInformation] 'PARC', '002077770'
I want the sp to work when one or the other parameter are either null, or blank.
This is what I have so far for code:
SET #s_SQL = 'SELECT d.ID, d.TransactionNumber, h.SourceType, ' + #s_ColumnName + ', d.FirstName, d.LastName,' +
'LTRIM(RTRIM(d.FirstName)) + '' '' + LTRIM(RTRIM(d.LastName)) [Name], '+
'd.PaymentAmount, CONVERT(VARCHAR(10), CAST(d.InitiationDate AS DATE), 101) [InitiationDate]' +
', d.Fee, d.TotalAmount, d.PaymentStatus, d.PaymentType, d.CreditCardType, ' +
'CONVERT(VARCHAR(10), CAST(d.PaymentEffectiveDate AS DATE), 101) [PaymentEffectiveDate]' +
', CONVERT(VARCHAR(10), CAST(d.ProcessDate AS DATE), 101) [ProcessDate], CONVERT(VARCHAR(10), CAST(d.CreatedDate AS DATE), 101) [CreatedDate],' +
'd.CashCode, d.TransConfirmID' +
', d.Phone, d.StreetAddress1, d.StreetAddress2, ' +
'LTRIM(RTRIM(d.StreetAddress1)) + '' '' + CASE WHEN LEN(d.StreetAddress2) > 0 THEN LTRIM(RTRIM(d.StreetAddress2)) ELSE '''' END [Address]' +
', d.City, d.[State], d.ZipFive, d.ZipFour, d.Email ' +
'FROM '+ #s_TableHeader + ' h WITH (NOLOCK) ' +
'INNER JOIN ' + #s_TableDetail + ' d WITH (NOLOCK) ' +
'ON h.ID = d.headerID ' +
'WHERE' +
' ((h.sourcetype = ' + '''' + #s_Source_Type + '''' + ') OR ' + '''' + #s_Source_Type + '''' + ' IS NULL OR ' + '''' + #s_Source_Type + '''' + '= '''')' +
' AND ((d.transconfirmid = ' + '''' + #s_Confirmation_Number + '''' + ') OR ' + '''' + #s_Confirmation_Number + '''' + ' IS NULL OR ' + '''' + #s_Confirmation_Number + '''' + '= '''')'
Any help I can get to figure why my checks are not working, it would be great.
*Please note that your example sql code is incomplete and that you name the parameters slightly different from the variables which makes for difficulty understanding the question. *
It is not without reason that the use of dynamic sql is discouraged even in cases where the dangers of sql injection are negligible. One of the primary reasons for this is that dynamic sql is difficult to write, read and debug. That being said I have often found myself using it to solve for problems within poorly designed systems.
I assume that you have investigated alternatives appropriately.
To reduce the complexity of dynamic sql statements I have found that constructing the statement in a modular way to be a good strategy.
In your particular case the use of an 'if' statement (or some variation) may help reduce the complexity of your dynamic where clause and likely help resolve your problem.
Example:
`
set #sql = 'select....... your select statement..... where'
if (#a_Source_Type is not null and len(#a_Source_Type) > 0)
begin
set #sql += '''' + #a_Source_Type + ''' = some_field and '
end
else
begin
set #sql += ' len(isnull(some_field, '''')) = 0 and '
end
set #sql += ' 1 = 1 '
`
The above attempts to move the comparison operators out of the dynamic sql.
I suggest refactoring your query to use a strategy similar to this example as you may find it easier to identify erroneous code.
the final line of the example has proven useful in circumstances where the resulting dynamic sql statement may or may not have where clauses to append
Try changing your WHERE to something like this:
'where' +
' h.sourcetype = ' + isnull(quotename(nullif(#s_Source_Type, ''), ''''), 'h.sourcetype') +
' AND d.transconfirmid = ' + isnull(quotename(nullif(#s_Confirmation_Number, ''), ''''), 'd.transconfirmid')

How to Automate an Insert "UnKnown" row to the Dimension tables in MSSQL Server?

I have multiple dimension tables and insert script for all tables. i want to insert 1st row as "UNKNOWN" row. when we deploy all the dimension tables for multiple client the UNKNOWN row also should be inserted so How can i do rather manual insertion ?
INSERT INTO Dimension.table1(Column1, Column2,Column3, Effective)
VALUES ('<Unknown>', '<Unknown>', '<Unknown>',GetDate());
INSERT INTO Dimension.[Table2] (Column_A, Column_B,Column_C,Column_D, Column_E, Effective)
VALUES (0,'<Unknown>',0, '<Unknown>','<Unknown>', GetDate());
INSERT INTO Dimension.[Table3] (ColumnX, ColumnY,ColumnX, Effective)
VALUES ('<Unknown>', '<Unknown>', '<Unknown>',GetDate());
I have to automate this script for multiple client.
There are two ways you can do this.
Create an insert script for every single table. This is easier to program, but requires additional work for every new dimension added.
Create a script that dynamically identifies the dimensions, the key value and the appropriate 'Unknown' value based on data type for each attribute. This can be very complicated to create, but is self maintaining as long as you don't add any new data types.
I expect the best choice will entirely depend on how many dimension tables you have, and how often you add new ones.
UPDATED
For what its worth, here is a stored procedure I wrote a few years ago. I'm sure it could use some updating, and for some reason my system is no longer recognizing the line break characters, but it should get you pretty close.
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
ALTER PROCEDURE [dbo].[InsertDimSeedRecords]
/*
-----------------------------------------------------------------------------------------------------------
Object Name: dbo.InsertDimSeedRecords
Project: Enterprise Data Warehouse
Purpose: Used to add seed rows to dimension tables, identified as tables prefixed with
Dim or contained in the dim schema.
Detailed description:
1. Identifies all dimension tables based on the Table and Schema parameters
2. Drops Constraints preventing changes to table data
3. Deletes seed values or Truncates tables based on TruncateData parameter
4. Generates new seed value insert statements
5. Inserts new records into table.
6. Re-create constraints
7. Enable constraints (currently disabled)
Example:
EXEC dbo.InsertDimSeedRecords
#DimTableName = 'All',
#Schema = 'All',
#DisplayResults = 'Y',
#ExecuteActions = 'N'
Parameters:
DimTableName - Accepts the Table Name by itself or qualified with the schema name for backward compatibility.
Values 'DimWorkOrder' and 'dbo.DimWorkOrder' will affect the same table.
'All' can be used to affect all dim tables in the specified schema.
#Schema - The schema name to affect. Ex: 'dim','dbo'; Default is 'dbo'
#TruncateData - If 'Y', truncates the dimension tables prior to reseeding.
If 'N', deletes the seed values (negative SK) from the table and inserts newly generated seed values.
Default is 'N'
#DisplayResults - If 'Y', displays a detailed list of all actions executed/planned, including successful actions.
If 'N', Successful actions are not displayed. Default = 'N'
#DisplayErrors - If 'Y', displays all ations that failed to execute, with error numbers and return codes
If 'N', no error details are returned, a ReturnCode is the only result set.
Use 'N' in automated or scripted tasks
Default is 'N'
#ExecuteActions - If 'N', all the tasks will be generated, but not executed. Use this to check what actions would
be performed without making any changes.
If 'Y', all tasks will be executed.
Default is 'Y' to allow for backward compatibility with previous versions
-----------------------------------------------------------------------------------------------------------
Rev | Date Modified | Developer | Change Summary
-----------------------------------------------------------------------------------------------------------
1 | 2013-11-01 | XXXXXXXX | Created
*/
#DimTableName SYSNAME,
#Schema SYSNAME = 'dbo',
#TruncateData CHAR(1) = 'N',
#DisplayResults CHAR(1) = 'N',
#DisplayErrors CHAR(1) = 'N',
#ExecuteActions CHAR(1) = 'Y'
AS
BEGIN
--/*
--*/
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#ErrorLog') IS NOT NULL
BEGIN
DROP TABLE #ErrorLog;
END
CREATE TABLE #ErrorLog (
TableObject NVARCHAR(256),
StepNum INT,
StepCommand NVARCHAR(MAX),
ErrorMessage NVARCHAR(4000),
ErrorNumber INT,
ErrorSeverity INT,
ErrorState INT,
ErrorLine INT,
ErrorProcedure NVARCHAR(200)
);
DECLARE #TableNameCRSR SYSNAME,
#SchemaNameCRSR SYSNAME;
DECLARE Table_Cursor CURSOR --Retrieve all tables to be affected
FOR
SELECT SCHEMA_NAME(o.schema_id),
o.NAME
FROM sys.objects AS O
WHERE o.type = 'U' --U = User Table
AND (
--All Dim tables from All eligible schemas
(
#Schema = 'All'
AND (
SCHEMA_NAME(o.schema_id) = 'dim'
OR o.NAME LIKE 'Dim%'
)
AND SCHEMA_NAME(o.schema_id) <> 'ref'
)
--tables from a specific schema
OR (
SCHEMA_NAME(o.schema_id) = #Schema
AND (
-- A specific table
o.NAME = #DimTableName
-- All dim tables
OR (
#DimTableName = 'All'
AND (
o.NAME LIKE '%Dim%'
OR #Schema = 'dim'
)
)
)
)
OR (SCHEMA_NAME(o.schema_id) + '.' + o.NAME = #DimTableName) --For legacy Schema Qualified #DimTableName values
)
AND NOT o.NAME IN (
--Add excluded/protected tables here
'MasterPackageControl', 'MasterPackageControl_Archive', 'ETLConfiguration', 'Date', 'Time'
);
OPEN Table_Cursor;
FETCH NEXT
FROM Table_Cursor
INTO #SchemaNameCRSR,
#TableNameCRSR
DECLARE #SQLCommand VARCHAR(MAX) = '',
#ProcOrder CHAR(1);
WHILE ##Fetch_Status = 0
BEGIN
DECLARE Worker_Cursor CURSOR
--Cursor to hold all of the commands in the correct processing order;
--this ensures that data is modified after constraints are dropped and before they are re-created,
--and that no constraint definitions are lost
FOR
WITH SQLConstraints
AS (
SELECT SQLDrop = 'RAISERROR (N''Dropping CONSTRAINT ' + OBJECT_NAME(fkc.constraint_object_id) + ''' , 10, 1) WITH NOWAIT;
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = Object_ID(N''[' + OBJECT_SCHEMA_NAME(fkc.parent_object_id) + '].[' + OBJECT_NAME(fkc.constraint_object_id) + ']'') AND parent_object_id =OBJECT_ID(N''[' + OBJECT_SCHEMA_NAME(fkc.parent_object_id) + '].[' + OBJECT_NAME(fkc.parent_object_id) + ']'') ' + ')
BEGIN
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fkc.parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(fkc.parent_object_id)) + ' DROP CONSTRAINT ' + QUOTENAME(OBJECT_NAME(fkc.constraint_object_id)) + '' + CHAR(13) + CHAR(10) + 'END
',
SQLDisable = 'RAISERROR (N''Disabling CONSTRAINT ' + OBJECT_NAME(fkc.constraint_object_id) + ''' , 10, 1) WITH NOWAIT;
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = Object_ID(N''[' + OBJECT_SCHEMA_NAME(fkc.parent_object_id) + '].[' + OBJECT_NAME(fkc.constraint_object_id) + ']'') AND parent_object_id =OBJECT_ID(N''[' + OBJECT_SCHEMA_NAME(fkc.parent_object_id) + '].[' + OBJECT_NAME(fkc.parent_object_id) + ']'') ' + ')
BEGIN
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fkc.parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(fkc.parent_object_id)) + ' NOCHECK CONSTRAINT ' + QUOTENAME(OBJECT_NAME(fkc.constraint_object_id)) + '' + CHAR(13) + CHAR(10) + 'END
' --+ ' ON DELETE CASCADE'
,
SQLAdd = 'RAISERROR (N''Creating CONSTRAINT [FK_' + OBJECT_NAME(fkc.parent_object_id) + '_' + C.NAME + '_' + OBJECT_SCHEMA_NAME(fkc.referenced_object_id) + OBJECT_NAME(fkc.referenced_object_id) --+ '_' + RC.name
+ ']'' , 10, 1) WITH NOWAIT;
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fkc.parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(fkc.parent_object_id)) + ' WITH NOCHECK ADD CONSTRAINT [FK_' + OBJECT_NAME(fkc.parent_object_id) + '_' + C.NAME + '_' + OBJECT_SCHEMA_NAME(fkc.referenced_object_id) + OBJECT_NAME(fkc.referenced_object_id) --+ '_' + RC.name
+ '] FOREIGN KEY(' + QUOTENAME(C.NAME) + ') REFERENCES ' + QUOTENAME(OBJECT_SCHEMA_NAME(fkc.referenced_object_id)) + '.' + QUOTENAME(OBJECT_NAME(fkc.referenced_object_id)) + '(' + QUOTENAME(RC.NAME) + ')
'
--+ ' ON DELETE CASCADE'
,
LenConstraintName = LEN('FK_' + OBJECT_NAME(fkc.parent_object_id) + '_' + C.NAME + '_' + OBJECT_SCHEMA_NAME(fkc.referenced_object_id) + OBJECT_NAME(fkc.referenced_object_id)),
SQLEnable = 'RAISERROR (N''Enabling CONSTRAINT [FK_' + OBJECT_NAME(fkc.parent_object_id) + '_' + C.NAME + '_' + OBJECT_SCHEMA_NAME(fkc.referenced_object_id) + OBJECT_NAME(fkc.referenced_object_id) --+ '_' + RC.name
+ ']'' , 10, 1) WITH NOWAIT;
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fkc.parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(fkc.parent_object_id)) + ' WITH CHECK CHECK CONSTRAINT [FK_' + OBJECT_NAME(fkc.parent_object_id) + '_' + C.NAME + '_' + OBJECT_SCHEMA_NAME(fkc.referenced_object_id) + OBJECT_NAME(fkc.referenced_object_id) + ']
',
NumRank = ROW_NUMBER() OVER (
ORDER BY OBJECT_SCHEMA_NAME(fkc.parent_object_id),
OBJECT_NAME(fkc.parent_object_id)
),
ReferencedTable = OBJECT_NAME(fkc.referenced_object_id),
ReferencedColumn = RC.NAME
FROM sys.foreign_key_columns AS FKC
INNER JOIN sys.columns AS C
ON fkc.parent_object_id = c.object_id
AND fkc.parent_column_id = c.column_id
INNER JOIN sys.columns AS RC
ON fkc.referenced_object_id = rc.object_id
AND fkc.referenced_column_id = rc.column_id
WHERE OBJECT_SCHEMA_NAME(fkc.referenced_object_id) = #SchemaNameCRSR
AND OBJECT_NAME(fkc.referenced_object_id) = #TableNameCRSR
),
SQLTables
AS (
SELECT SQLTruncate = '
RAISERROR (N''TRUNCATE TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + ''' , 10, 1) WITH NOWAIT;
TRUNCATE TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) + '
',
NumRank = ROW_NUMBER() OVER (
ORDER BY TABLE_SCHEMA,
TABLE_NAME
),
*
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_TYPE = 'Base Table'
AND TABLE_SCHEMA = #SchemaNameCRSR
AND TABLE_NAME = #TableNameCRSR
),
SQLInserts
AS (
SELECT DISTINCT SQLInsert = 'RAISERROR (N''Inserting ' + QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME) + ''' , 10, 1) WITH NOWAIT;
' + CASE
WHEN OBJECTPROPERTY(OBJECT_ID(QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME)), 'TableHasIdentity') = 1
THEN 'SET IDENTITY_INSERT ' + QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME) + ' ON
'
ELSE ''
END + '
INSERT INTO ' + QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME) + '
( ' + REPLACE(SUBSTRING((
SELECT ', ' + QUOTENAME(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS C2
WHERE c2.TABLE_NAME = c1.Table_name
AND c2.TABLE_SCHEMA = c1.table_schema
ORDER BY ORDINAL_POSITION
FOR XML PATH('')
), 2, 16000) + '
)', ', ', CHAR(13) + SPACE(10) + ', ') + '
VALUES
(
' + REPLACE(SUBSTRING((
SELECT ', ' + CASE
WHEN c2.column_name IN ('ModifiedDate', 'UpdateAuditKey')
THEN 'Null --' + c2.COLUMN_NAME
WHEN C2.COLUMN_NAME IN ('CreatedDate', 'EffectiveDate')
THEN '''' + CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 120) + ''' --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('InsertAuditKey')
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('CurrentIndicator')
THEN '1 --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('RowStamp')
THEN '''-1'' --' + c2.COLUMN_NAME
WHEN c2.data_type = 'bit'
THEN '1 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%char%'
OR c2.data_type = 'ntext'
THEN CASE
WHEN c2.character_maximum_length > 7
THEN '''Unknown'''
WHEN c2.character_maximum_length > 2
THEN '''Unk'''
ELSE '''U'''
END + ' --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%date%'
THEN '''19000101'' --' + c2.column_name
WHEN c2.data_type LIKE 'tinyint'
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%int%'
THEN '-1 --' + c2.COLUMN_NAME
WHEN c2.data_type IN ('numeric', 'decimal', 'float')
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%binary%'
THEN 'cast(-1 as ' + DATA_TYPE + ISNULL('(' + CAST(Character_Maximum_length AS VARCHAR(5)) + '))', '') + ' --' + c2.COLUMN_NAME
END
FROM INFORMATION_SCHEMA.COLUMNS AS C2
WHERE c2.TABLE_NAME = c1.Table_name
AND c2.TABLE_SCHEMA = c1.table_schema
ORDER BY ORDINAL_POSITION
FOR XML PATH('')
), 2, 16000) + '
),
', ', ', CHAR(13) + SPACE(10) + ', ') + '
(
' + REPLACE(SUBSTRING((
SELECT ', ' + CASE
WHEN c2.column_name IN ('ModifiedDate', 'UpdateAuditKey')
THEN 'Null --' + c2.COLUMN_NAME
WHEN C2.COLUMN_NAME IN ('CreatedDate', 'EffectiveDate')
THEN '''' + CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 120) + ''' --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('InsertAuditKey')
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('CurrentIndicator')
THEN '1 --' + c2.COLUMN_NAME
WHEN c2.column_name IN ('RowStamp')
THEN '''-2'' --' + c2.COLUMN_NAME
WHEN c2.data_type = 'bit'
THEN '1 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%char%'
OR c2.data_type = 'ntext'
THEN CASE
WHEN c2.character_maximum_length > 13
THEN '''Not Applicable'''
WHEN c2.character_maximum_length > 2
THEN '''N/A'''
ELSE '''N'''
END + ' --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%date%'
THEN '''18991231'' --' + c2.column_name
WHEN c2.data_type LIKE 'tinyint'
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%int%'
THEN '-2 --' + c2.COLUMN_NAME
WHEN c2.data_type IN ('numeric', 'decimal', 'float')
THEN '0 --' + c2.COLUMN_NAME
WHEN c2.data_type LIKE '%binary%'
THEN 'cast(-2 as ' + DATA_TYPE + ISNULL('(' + CAST(Character_Maximum_length AS VARCHAR(5)) + '))', '') + ' --' + c2.COLUMN_NAME
END
FROM INFORMATION_SCHEMA.COLUMNS AS C2
WHERE c2.TABLE_NAME = c1.Table_name
AND c2.TABLE_SCHEMA = c1.table_schema
ORDER BY ORDINAL_POSITION
FOR XML PATH('')
), 2, 16000) + '
)
' + CASE
WHEN OBJECTPROPERTY(OBJECT_ID(QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME)), 'TableHasIdentity') = 1
THEN 'SET IDENTITY_INSERT ' + QUOTENAME(c1.TABLE_SCHEMA) + '.' + QUOTENAME(c1.TABLE_NAME) + ' OFF
'
ELSE ''
END + '
', ', ', CHAR(10) + CHAR(13) + SPACE(10) + ', ')
FROM INFORMATION_SCHEMA.COLUMNS AS C1
INNER JOIN INFORMATION_SCHEMA.TABLES AS T
ON C1.TABLE_CATALOG = T.TABLE_CATALOG
AND C1.TABLE_SCHEMA = T.TABLE_SCHEMA
AND C1.TABLE_NAME = T.TABLE_NAME
WHERE 1 = 1
AND t.TABLE_TYPE <> 'VIEW'
AND t.TABLE_SCHEMA = #SchemaNameCRSR
AND t.TABLE_NAME = #TableNameCRSR
),
SQLCleanKey
AS (
SELECT SQLCommand = 'RAISERROR (N''Delete keys from ' + QUOTENAME(tc.TABLE_SCHEMA) + '.' + QUOTENAME(tc.TABLE_NAME) + ''' , 10, 1) WITH NOWAIT;
DELETE FROM ' + QUOTENAME(TC.TABLE_SCHEMA) + '.' + QUOTENAME(TC.TABLE_NAME) + ' WHERE ' + QUOTENAME(ccu.COLUMN_NAME) + ' < 0'
FROM INFORMATION_SCHEMA.constraint_column_usage AS CCU
INNER JOIN INFORMATION_SCHEMA.table_constraints AS TC
ON CCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
AND CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND CCU.CONSTRAINT_CATALOG = TC.CONSTRAINT_CATALOG
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND tc.TABLE_SCHEMA = #SchemaNameCRSR
AND tc.TABLE_NAME = #TableNameCRSR
),
CommandsUnioned
AS (
SELECT SQLCommand = SQLDrop,
ProcOrder = 1
FROM SQLConstraints
WHERE #TruncateData = 'Y'
UNION ALL
SELECT SQLDisable,
ProcOrder = 2
FROM SQLConstraints
WHERE #TruncateData = 'N'
UNION ALL
SELECT SQLTruncate,
ProcOrder = 3
FROM SQLTables
WHERE #TruncateData = 'Y'
UNION ALL
SELECT SQLCommand,
ProcOrder = 4
FROM SQLCleanKey
WHERE #TruncateData = 'N'
UNION ALL
SELECT SQLInsert,
ProcOrder = 5
FROM SQLInserts
UNION ALL
SELECT SQLAdd,
ProcOrder = 6
FROM SQLConstraints
WHERE #TruncateData = 'Y'
UNION ALL
SELECT sqlenable,
procorder = 7
FROM sqlconstraints
)
SELECT SQLCommand,
ProcOrder
FROM CommandsUnioned
ORDER BY ProcOrder,
SQLCommand;
OPEN Worker_Cursor
FETCH NEXT
FROM Worker_Cursor
INTO #SQLCommand,
#ProcOrder
WHILE ##Fetch_Status = 0
BEGIN
BEGIN TRY
IF #ExecuteActions = 'Y'
BEGIN
EXECUTE (#SQLCommand);
END
INSERT INTO #ErrorLog (
TableObject,
StepNum,
StepCommand,
ErrorMessage,
ErrorNumber,
ErrorSeverity,
ErrorState,
ErrorLine,
ErrorProcedure
)
VALUES (
QUOTENAME(#SchemaNameCRSR) + '.' + QUOTENAME(#TableNameCRSR),
CAST(#ProcOrder AS INT),
#SQLCommand,
ISNULL(ERROR_MESSAGE(), '-'),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_LINE(),
ISNULL(ERROR_PROCEDURE(), '-')
)
END TRY
BEGIN CATCH
INSERT INTO #ErrorLog (
TableObject,
StepNum,
StepCommand,
ErrorMessage,
ErrorNumber,
ErrorSeverity,
ErrorState,
ErrorLine,
ErrorProcedure
)
VALUES (
QUOTENAME(#SchemaNameCRSR) + '.' + QUOTENAME(#TableNameCRSR),
CAST(#ProcOrder AS INT),
#SQLCommand,
ERROR_MESSAGE(),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_LINE(),
ISNULL(ERROR_PROCEDURE(), '-')
)
END CATCH
FETCH NEXT
FROM Worker_Cursor
INTO #SQLCommand,
#ProcOrder
END
CLOSE Worker_Cursor;
DEALLOCATE Worker_Cursor;
FETCH NEXT
FROM Table_Cursor
INTO #SchemaNameCRSR,
#TableNameCRSR
END
CLOSE Table_Cursor;
DEALLOCATE Table_Cursor;
DECLARE #ret_code INT;
SELECT #ret_code = MAX(ISNULL(ErrorNumber, 0))
FROM #ErrorLog;
IF #DisplayResults = 'Y'
OR #ret_code <> 0
BEGIN
SELECT TableObject,
StepNum,
ErrorMessage,
ErrorNumber,
ErrorSeverity,
ErrorState,
ErrorLine,
ErrorProcedure,
StepCommand
FROM #ErrorLog
WHERE (
(
#DisplayErrors = 'Y'
AND ErrorNumber IS NOT NULL
)
OR #DisplayResults = 'Y'
)
ORDER BY StepNum,
TableObject,
StepCommand
END
RETURN (#ret_code)
END
GO

Script to create an insert SQL

I'm trying to create a script that concatenate various values that in the end it creates a insert script,i'm already made one.
INSERT INTO #SCRPTS
SELECT getdate(),
'INSERT WKF_TpProcesso VALUES (''' + Des_TpProcesso + ''',' + cast(Cod_Func as Varchar(8)) + ',getdate() ,null ,1 ,' + CASE WHEN Id_Sistema IS NULL THEN 'Null' ELSE CAST(Id_Sistema as Varchar(6)) END
+',0,1,0,'+ CASE WHEN Des_CodigoTpProcesso IS NULL THEN 'Null' ELSE '''' + Des_CodigoTpProcesso END +''''+ ','+CAST(Flg_ProxProcesso as Varchar(1))''')'
FROM WKF_TpProcesso
WHERE Des_CodigoTpProcesso = #Des_CodigoTpProcesso
The result is this:
INSERT WKF_TpProcesso VALUES ('Processo Teste',41815,getdate() ,null ,1 ,169,0,1,0, 'PcTestPlus',1)
I'm doing another but can't figure out why doesn't work.
WHILE (exists (select Id_EtapaCampoGen from WKF_EtapaCampoGen Where Id_Etapa = #Id_Etapa ))
INSERT INTO #SCRPTS
SELECT getdate(),
'INSERT WKF_EtapaCampoGen VALUES
(#Id_Etapa' +
','+ ''''+ Des_Label + '''' +
','+ ''''+ Vl_Identificador + '''' +
','+ cast(Flg_Obrigatorio as Varchar(2)) +
',getdate(),'+
',' + CASE WHEN DT_Alteracao IS NULL OR DT_Alteracao = '' THEN 'NULL' ELSE '''' +
',' + cast(FLG_Situacao as Varchar(2))+
',' + '''' + Tipo + '''' +
',' + '''' + Query + '''' +')'
FROM WKF_EtapaCampoGen
I'm having error "Incorrect sintax near FROM",but i cant see what is wrong,someone knows ?
What is funny is that for example the column Des_Label in this is script the DB find the reference of where it comes from,but if i have the error in the from statment how it can find?
It was a END missing in the case when stament,with the end the script returned this.
INSERT WKF_EtapaCampoGen VALUES
(#Id_Etapa,'Areas','CampoComplementar1',0,getdate(),NULL,1,'Lista','select Des_area as Texto, Des_area as valor from wkf_area')
your case expression needs an END
CASE WHEN DT_Alteracao IS NULL OR DT_Alteracao = '' THEN 'NULL' ELSE '''''' END

SQL Server : execute dynamic sql help please

EXEC SP_EXECUTESQL
#DynamicSQL
, N'#HostIDs VARCHAR(MAX) OUTPUT'
, #HostIDs OUTPUT;
PRINT #HostIDs;
SELECT #HostIDs AS HostIDs;
SET #UpdateSQL = '
EXECUTE [dbo].[usp_Win7_HostUpdater_NEW]
#HostID = ''' + #HostIDs + ''' ,
#PackageID = ''' + #PackageID + ''' ,
#MigrationFlag = ''' + #MigrationFlagID + ''' ,
#Manufacturer = ' + #Manufacturer + ' ,
#Product = ' + #Product + ' ,
#Version = ' + #Version + ' ,
#Reason = ' + #Reason + ' ,
#Contact = ' + #Contact + '
';
SELECT #UpdateSQL AS UpdateSQL;
PRINT #UpdateSQL;
EXEC( #UpdateSQL )
END
I have a stored procedure on both a SQL Server 2005 and 2008 in which the above code is the last part of
it returns a VARCHAR(MAX) of numbers separated by commas.
Now this returned value is large upwards of 600k characters. If I execute this on a SQL Server 2005 it works like 50% of the time, #HostIDs is populated always and #UpdateSQL gets generated with the correct values and is executed.
On SQL Server 2008, #HostIDs is populated but #UpdateSQL is always NULL
This is weirding me out tremendously
Can anyone maybe shed some light on my odd problem?
Check these out
SET CONCAT_NULL_YIELDS_NULL OFF
select 'abc' + null + 'def'
--- abcdef
SET CONCAT_NULL_YIELDS_NULL ON
select 'abc' + null + 'def'
--- NULL
That's one way to get around the problem, which is to set it off before your string building and back on after. Any NULL in the sequence of string concatenation renders the entire statement NULL, which explains it works like 50% of the time - these are when all of the variables are non-null.
Completely agree with freefaller though, unless the question's an abstraction of a larger puzzle, there's no reason to build a dynamic SQL and EXEC it when a direct execution will work for the particular snippet shown.
If any of the parameters are null, the entire statement will be null. You can work around it by doing something like this (and I don't know what the data types are, but sometimes you need to cast them to varchar from int/bool/etc. types to make the concatenation work):
SET #UpdateSQL = '
EXECUTE [dbo].[usp_Win7_HostUpdater_NEW]
#HostID = ' + ISNULL('''' + #HostIDs + '''', 'null') + ' ,
#PackageID = ' + ISNULL('''' + #PackageID + '''', 'null') + ' ,
#MigrationFlag = ' + ISNULL('''' + #MigrationFlagID + '''', 'null') + ' ,
#Manufacturer = ' + ISNULL(#Manufacturer, 'null') + ' ,
#Product = ' + ISNULL(#Product, 'null') + ' ,
#Version = ' + ISNULL(#Version, 'null') + ' ,
#Reason = ' + ISNULL(#Reason, 'null') + ' ,
#Contact = ' + ISNULL(#Contact, 'null') + '
';
It causes because you are not handling nulls
you can use sp_executesql instead of exec it has some benefits over exec

Resources