0
alter procedure NewUserTableCreation(@Username varchar(50))
as
    declare @CreateUserTable NVARCHAR(MAX)
    declare @AddRecord NVARCHAR(MAX)
    declare @VisitedClothesKids varchar(50)='VisitedClothesKids'
    declare @InitialCount varchar(20)='0'
BEGIN
    --Building query for creating a user table
    SET @CreateUserTable = 'create table ' + @Username +
                           '_Table(UserActivityData varchar(50),DataValue varchar(20))'
    EXEC(@CreateUserTable);

    --Adding Records in the user table
    SET @AddRecord = 'insert into ' + @Username + '_Table(UserActivityData, DataValue)
                      values(' + @VisitedClothesKids + ','  + @InitialCount + ')'
    EXEC(@AddRecord);
END
GO

I'm executing this procedure from C# code. A table is successfully created and then an exception is thrown saying,

Invalid column name 'VisitedClothesKids'
Invalid column name 'InitialCount'

Please help! Many Thanks :)

0

2 Answers 2

2

The issue is that the string being concatenated itself is not putting the values in quotes:

values('+@VisitedClothesKids+','+@InitialCount+')'

becomes

values(VisitedClothesKids,0)'

when you want it to be

values('VisitedClothesKids','0')'

We should also warn you that the technique you are using here is open to SQL Injection and should be avoided.

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

1 Comment

Thanks for the clue! :D
0

To solve this kind of problem, you have to review the statement that your are executing. For example, if you did this (passing in "MyData" for a table name):

PRINT @AddRecord;
EXEC(@AddRecord);

You would see the following as output:

insert into MyData_Table(UserActivityData,DataValue) values(VisitedClothesKids,0)'

Which fails because SQL doesn't know what "VisitedClothesKids" is. You want the statement to be

insert into MyData_Table(UserActivityData,DataValue) values('VisitedClothesKids',0)'

with the quotes to designate the literal string. To get this, modify your "build statement like so:

SET @AddRecord = 'insert into '+@Username+'_Table(UserActivityData,DataValue)
values('''+@VisitedClothesKids+''','+@InitialCount+')'

In this context, SQL will interpret (or "escape") the two single-quotes, '' as a single quote, '.

2 Comments

Definitely, the code has both design and security issues. I'm only addressing the "why this particular case" fails issue, with tips on how to catch similar future problems.
Thank You Andrew and Philip. The problem has been solved. Yes, I'm using stored procedures to prevent SQL injection. Sad that the code is still vulnerable. Well, this is form my academic mini project (with deadline of course) that is why I'm less concerned about the security. I'll soon learn how to design it in a more secure way. Anyway, thanks again!

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.