Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
Alter procedure VisitorDetails1
@p_name nvarchar(50),
@p_City nvarchar(100),
@p_Dept nvarchar(max),
@p_TableName nvarchar(50)
as

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@p_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 '+@p_TableName+'(Name,City,Dept) values(@p_name,@p_City,@p_Dept)'

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

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


--Parameters Execution Part:
exec VisitorDetails1 Perumal,Chennai,IT,VSampleData--(This Is Table Name I created)

After Executing Result IS:

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@p_".
Success..

Actually I need to Check the table is already exist or not.. if exist in that same table the data should be inserted other wise it should execute Table is Not There. These all should dynamically..


Please Help Me what I need to Do..
Thanks Advace,.
Posted
Updated 7-Jan-13 2:01am
v2
Comments
Aarti Meswania 7-Jan-13 7:57am    
do not repost questions
http://www.codeproject.com/Answers/523251/ASP-NETplusSqlplusServerplusInplusStoredplusProced#answer3
use solution-3 it will work

your store procedure is correct just modify Query as I have mentioned
you need not to change parameter @name to @P_name
It never metters

Firs, you are reposting your question. The solution to one of your problem is Here[^]. You should improve your question if there is additional doubt or clarification. Second, the flow of your stored procedure should go like this:
SQL
Alter procedure VisitorDetails1
@p_name nvarchar(50),
@p_City nvarchar(100),
@p_Dept nvarchar(max),
@p_TableName nvarchar(50)
as
BEGIN
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@p_TableName)) 
BEGIN
--Declare @set nvarchar(50)
Declare @set nvarchar(500)
--Replace your query to 
set @set='insert into '+@TableName+'(Name,City,Dept) values(''' + @name + ''',''' + @City + ''',''' + @Dept + ''')'
Print 'Success'
END
else
BEGIN
Print 'Table is Not there'
END
END
 
Share this answer
 
Comments
__TR__ 7-Jan-13 8:19am    
+5
Zafar Sultan 7-Jan-13 8:47am    
Thanks.
I guess the statement should be:

set @set='insert into '+@TableName+'(Name,City,Dept) values(''' + @name + ''',''' + @City + ''',''' + @Dept + ''')'
 
Share this answer
 
v2
Comments
__TR__ 7-Jan-13 8:18am    
+5.
Congratulations for winning MVP 2013:)
Manas Bhardwaj 7-Jan-13 8:29am    
thx :)

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