Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Alter procedure VisitorDetails1
@name nvarchar(50),
@City nvarchar(100),
@Dept nvarchar(max),
@TableName nvarchar(50)
as

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@TableName)) 
--IF db_id('@TabName') IS NOT NULL
--IF OBJECT_ID('@TabName','U') is not null
begin

Declare @set nvarchar(50)
set @set='insert into '+@TableName+'(Name,City,Dept) values(@name,@City,@Dept)'

--set @set='insert into '+@TableName+' values(@name,@City,@Dept)'

exec(@set)
Print 'Success'
end
else
begin
print 'Table is Not there'
end


My Result IS After Exectuing:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@name".
Success..



Please Help Me What I need to Do..
Advance Thanks.
Posted
Updated 7-Jan-13 1:32am
v2

SQL
Alter procedure VisitorDetails1
@name nvarchar(50),
@City nvarchar(100),
@Dept nvarchar(max),
@TableName nvarchar(50)
as
 
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@TableName)) 
--IF db_id('@TabName') IS NOT NULL
--IF OBJECT_ID('@TabName','U') is not null
begin
 
Declare @set nvarchar(50)
set @set='insert into '+@TableName+'(Name,City,Dept) values(''' + @name + ''',''' + @City + ''',''' + @Dept + ''')'
 
exec(@set)
Print 'Success'
end
else
begin
print 'Table is Not there'
end

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Zafar Sultan 7-Jan-13 7:43am    
+5
Aarti Meswania 7-Jan-13 7:44am    
thank you! :)
__TR__ 7-Jan-13 8:16am    
5ed!
Aarti Meswania 7-Jan-13 8:17am    
Thank you!
:)
URVISH_SUTHAR1 7-Jan-13 11:08am    
Easy to pass "SQL Injection" on this prod. So think about it :)
This will not work
Exec statement execute in different context where the variables are not available.

instead concatenate the values to the string and exectuee

for exgs

Declare @str nVarchar(50)

set @str ='Insert into Tb1 values(' + @r + ')'
exec(@str)
 
Share this answer
 
Comments
Perumalkutti 7-Jan-13 8:10am    
I Tried This also... But Did't Work Sir...
Change this
SQL
set @set='insert into '+@TableName+'(Name,City,Dept) values(@name,@City,@Dept)'
to
SQL
set @set='insert into '+@TableName+'(Name,City,Dept) values('+@name+','+@City+','+@Dept+')'


Change the above sql statement as per your requirement. You will need to wrap your string in apostrophes.
 
Share this answer
 
Comments
Perumalkutti 7-Jan-13 8:09am    
I Tried This Also sir.. But didt work
Zafar Sultan 7-Jan-13 8:16am    
You are jumping from one thread to another. Stick to one. The complete solution to your problem is posted here: http://www.codeproject.com/Questions/523255/ASP-NETplusStoredplusPRocedures Now do not keep updating both the threads. Use one only. Please.
Hi,
change your variable name
@name to @p_name

do this to all......
it will definitely work..
 
Share this answer
 
v2
Comments
Perumalkutti 7-Jan-13 8:09am    
the same problem sir..
You have to make changes at 2 places. Highlighted in bright colors

one is change the size of @set variable.

When you declare @set=50, it can hold only 50 characters and if you try to insert it will fail


Alter procedure VisitorDetails1
@name nvarchar(50),
@City nvarchar(100),
@Dept nvarchar(max),
@TableName nvarchar(50)
as

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@TableName))
--IF db_id('@TabName') IS NOT NULL
--IF OBJECT_ID('@TabName','U') is not null
begin

Declare @set nvarchar(500)
set @set='insert into '+@TableName+'(Name,City,Dept) values(''' + @name + ''',''' + @City + ''',''' + @Dept + ''')'
print @set
exec(@set)
Print 'Success'
end
else
begin
print 'Table is Not there'
end



You have to make changes at 2 places
 
Share this answer
 
Comments
Perumalkutti 8-Jan-13 7:53am    
Ya I Got this after tried a lot.. But Good Solution for this Thanks a lot Sir..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900