0

When i do this

declare @cmd nvarchar(50)
set @cmd ='SELECT category_id FROM tbl_BHMCategoryMaster '
exec(@cmd)

query executes properly.

But When I do this

declare @cmd nvarchar(50)
set @cmd ='SELECT [category_id],[category_name],[Basic],[Dimension],[In_Out],[ProfileHand],[Glass],[Hinge],[Edgeband]  FROM [tbl_BHMCategoryMaster] ' 
exec(@cmd)

I get this error and i have no clue what i am doing wrong

Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'Dime'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Dime'.

But again when i tried this

 exec('SELECT [category_id],[category_name],[Basic],[Dimension],[In_Out],[ProfileHand],[Glass],[Hinge],[Edgeband]  FROM [tbl_BHMCategoryMaster]')

it worked properly.

There may be many alternative to this but i want to know the problem in detail so that i don't face it next time. If anyone can put some light it will be of great help. Thanks :)

2
  • 1
    Your nvarchar needs to be bigger than (50) Commented Sep 15, 2013 at 14:11
  • thanks for pointing out the mistake Laurence :) Commented Sep 15, 2013 at 14:19

1 Answer 1

2

You haven't declared @cmd to be long enough. Try this:

declare @cmd nvarchar(max);
set @cmd = N'SELECT [category_id],[category_name],[Basic],[Dimension],[In_Out],[ProfileHand],[Glass],[Hinge],[Edgeband]  FROM [tbl_BHMCategoryMaster] ' ;
exec(@cmd);
Sign up to request clarification or add additional context in comments.

1 Comment

that was so lame of me..Anyways thanks for pointing out the mistake Gordon :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.