August 18, 2014 at 1:06 am
Since you are mixing integer data types with string data types, the + operator is not used for string concatenation but for the mathematical addition.
And you can't "add" a string to an integer.
You need to convert the integers to strings.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
August 18, 2014 at 1:41 am
Hi Try this:
Print('select case when '''+@action+'''='''+@dbname+''' then '+cast(@a as varchar)+' else '+cast(@b as varchar)+' end')
Thanks,
Shiva N
Database Consultant
August 18, 2014 at 1:46 am
Shiva N (8/18/2014)
Hi Try this:Print('select case when '''+@action+'''='''+@dbname+''' then '+cast(@a as varchar)+' else '+cast(@b as varchar)+' end')
I consider leaving out length specifications for strings (and leaving it up to the default) a bad practice.
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + @dbname + ''' THEN' + CAST(@a AS VARCHAR(10)) + ' ELSE' + CAST(@b AS VARCHAR(10))+' END')
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
August 18, 2014 at 2:56 am
Quick thought, why going through all this trouble with the case statement in the dynamic sql?
😎
declare @b-2 int
declare @action varchar(100)
declare @dbname varchar(100)
declare @a int
set @dbname = 'adt'
set @action = 'dfgf'
set @a = 14
set @b-2 = 12
SELECT
CASE WHEN @dbname = @action THEN N'SELECT ' + CONVERT(VARCHAR(12),@a,3)
ELSE N'SELECT ' + CONVERT(VARCHAR(12),@b,3)
END
August 18, 2014 at 4:16 am
@Action already set in quotes ,then why od we need to have quotes again the dynamic query?
August 18, 2014 at 5:00 am
Koen Verbeeck (8/18/2014)
Shiva N (8/18/2014)
Hi Try this:Print('select case when '''+@action+'''='''+@dbname+''' then '+cast(@a as varchar)+' else '+cast(@b as varchar)+' end')
I consider leaving out length specifications for strings (and leaving it up to the default) a bad practice.
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + @dbname + ''' THEN' + CAST(@a AS VARCHAR(10)) + ' ELSE' + CAST(@b AS VARCHAR(10))+' END')
This is still throwng error saying inccorect syntax near CAST.. instead o fprint try with EXEC it throws error
August 18, 2014 at 5:04 am
This works fine:
DECLARE @b-2 INT;
DECLARE @action VARCHAR(100);
DECLARE @dbname VARCHAR(100);
DECLARE @a INT;
SET @dbname = 'adt';
SET @action = 'dfgf';
SET @a = 14;
SET @b-2 =12;
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + 'view' + ''' THEN ' + CAST(@a AS VARCHAR(10)) + ' ELSE ' + CAST(@b AS VARCHAR(10))+' END');
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
August 18, 2014 at 5:09 am
Koen Verbeeck (8/18/2014)
This works fine:
DECLARE @b-2 INT;
DECLARE @action VARCHAR(100);
DECLARE @dbname VARCHAR(100);
DECLARE @a INT;
SET @dbname = 'adt';
SET @action = 'dfgf';
SET @a = 14;
SET @b-2 =12;
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + 'view' + ''' THEN ' + CAST(@a AS VARCHAR(10)) + ' ELSE ' + CAST(@b AS VARCHAR(10))+' END');
Oh nooooooo this is throwing error dnt print it try executing it..USE EXEC instead of print
August 18, 2014 at 5:15 am
Indu-649576 (8/18/2014)
Koen Verbeeck (8/18/2014)
This works fine:
DECLARE @b-2 INT;
DECLARE @action VARCHAR(100);
DECLARE @dbname VARCHAR(100);
DECLARE @a INT;
SET @dbname = 'adt';
SET @action = 'dfgf';
SET @a = 14;
SET @b-2 =12;
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + 'view' + ''' THEN ' + CAST(@a AS VARCHAR(10)) + ' ELSE ' + CAST(@b AS VARCHAR(10))+' END');
Oh nooooooo this is throwing error dnt print it try executing it..USE EXEC instead of print
That's because you don't declare the variables in your dynamic SQL. EXEC creates a new execution context.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
August 18, 2014 at 5:19 am
Koen Verbeeck (8/18/2014)
Indu-649576 (8/18/2014)
Koen Verbeeck (8/18/2014)
This works fine:
DECLARE @b-2 INT;
DECLARE @action VARCHAR(100);
DECLARE @dbname VARCHAR(100);
DECLARE @a INT;
SET @dbname = 'adt';
SET @action = 'dfgf';
SET @a = 14;
SET @b-2 =12;
PRINT('SELECT CASE WHEN ''' + @action + '''=''' + 'view' + ''' THEN ' + CAST(@a AS VARCHAR(10)) + ' ELSE ' + CAST(@b AS VARCHAR(10))+' END');
Oh nooooooo this is throwing error dnt print it try executing it..USE EXEC instead of print
That's because you don't declare the variables in your dynamic SQL. EXEC creates a new execution context.
So I have to declare variables in side the dynamic query???correct the sqlcode and plz post it to get executed
August 18, 2014 at 5:20 am
Because you can't concatenate within an EXEC
DECLARE @b-2 INT;
DECLARE @action VARCHAR(100);
DECLARE @dbname VARCHAR(100);
DECLARE @a INT;
SET @dbname = 'adt';
SET @action = 'dfgf';
SET @a = 14;
SET @b-2 =12;
DECLARE @Result VARCHAR(500);
SET @Result = 'SELECT CASE WHEN ''' + @action + '''=''' + 'view' + ''' THEN ' + CAST(@a AS VARCHAR(10)) + ' ELSE ' + CAST(@b AS VARCHAR(10))+' END';
EXEC (@Result);
Should have mentioned you wanted to EXEC it earlier....
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply