3

I have this stored procedure that accept a comlumn name as inptu parameter. The SELECT statement will select a column according to input parameter

create procedure getColumn (@whichColumn varchar)
as
begin
    declare @sql nvarchar(max) 
    set @sql = 'SELECT [' + @whichColumn + ']' 
        + ' FROM myTable'
        + ' where ['+ @whichColumn + '] is not null'
        + ' and [' + @whichColumn + '] != '''' ' ;
    exec sp_executesql @sql
end

When I execute this stored procedure,

exec getColumn 'Apple';

the error says "Invalid column name 'A' " . I cannot see why it only gets the first character of the input

3 Answers 3

7

Check out your parameter declaration:

@whichColumn varchar

From MSDN:

When n is not specified in a data definition or variable declaration statement, the default length is 1.

So that's a single-letter varchar. Try to specify a size:

@whichColumn varchar(50)

Or even better, use the system-defined type for object names:

@whichColumn sysname
Sign up to request clarification or add additional context in comments.

5 Comments

^^^^This is your answer. Always declare the length of your columns.
I thought it was 30 if no length has been mentioned :S
@M.Ali: That's true if you omit the length in cast and convert. But in variable declarations, the default length is one. You can vote here to fix the madness.
opss got it mixed. :) I have tried to vote but it wont let me will try it later.
/sigh the fact that this isn't the accepted answer is troubling.
3
create procedure getColumn (@whichColumn nvarchar(128))   --<-- Maximum column name lenght
as
begin
    declare @sql nvarchar(max); 
    set @sql =   N'SELECT ' + QUOTENAME(@whichColumn)+ N' FROM myTable'
               + N' where '+ QUOTENAME(@whichColumn) + N' is not null'
               + N' and ' + QUOTENAME(@whichColumn)  +  N' != '''' ' ;
    exec sp_executesql @sql
end

On a side note Using square brackets in you concatinating string isnt the same as using QUOTENAME function.

4 Comments

DO you care to leave a comment whoever has down voted ?
+1 to off balance. Not sure why you got the DV either.
Cheers Zane there are some numpties out there down voting for fun. I wish there was a system in place that every time some down voted unfairly it would -100 their reputation :S
M. Ali I did not know about TSQL keyword QUOTENAME until I read your suggested answer. It solved the issue I was having writing dynamic SQL to set a column name in an UPDATE stored procedure. A bonus is it's cleaner to write and understand IMHO.
1

Change this line

create procedure getClumn (@whichColumn varchar)

to

create procedure getClumn (@whichColumn varchar(max))

because if you are not assign size of varchar at that time it consider only one character so it get only one character A and generate error.

2 Comments

Whoa let's not get carried away and go all the way to VARCHAR(MAX) my assumption is he probably needs less than 8,000 characters to accomplish this.
@Zane My assumption is hes never going to need more than 128 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.