When I use to execute and write the direct table creation query, it works fine. But when I set the same SQL to a variable and then execute the variable, it does.
EXECUTE ( 'IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N''DQ_ACC_' + @BUSINESS_NAME + '_OP_BASIC_PROFILE_ENTITY_TREND'') AND type = ''U'')
CREATE TABLE DQ_ACC_' + @BUSINESS_NAME + '_OP_BASIC_PROFILE_ENTITY_TREND
(
[BUSINESS_NAME] [VARCHAR](200) NULL,
[PROFILE_NUM] [INT] NULL,
[TABLE_NAME] [VARCHAR](200) NULL,
[ATTRIBUTE_COUNT] [INT] NULL,
[MIN_ROW_LENGTH] [INT] NULL,
[MAX_ROW_LENGTH] [INT] NULL,
[NUM_DUPLICATE_RECS] [INT] NULL,
[SUMMARY_DETAIL_VIEW] [VARCHAR](100) NULL,
[S_KEY] [INT] NULL,
[LOAD_TIMESTAMP] [DATETIME] NULL) ON [PRIMARY]'
);
When I assign the same query using the set command, it does not work.
Can you please tell me what I am doing wrong here?
@BUSINESS_NAMEwhether it has value ornull? cause ifnullit will not generate the script.