Click here to Skip to main content
15,905,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HHi ,

i am getting error while running following sql code :

create procedure insert_record
(
@table varchar(20),
@fields varchar(100),
@values varchar(max)
)
as
begin
insert into @table(@fields) values(@values)
end


Error: Msg 102, Level 15, State 1, Procedure insert_record, Line 9
Incorrect syntax near '@fields'.
Posted
Comments
ArunRajendra 26-Sep-13 1:23am    
Is @fields contains column name?

Try dynamic sql:

SQL
Createprocedure insert_record
(
@table varchar(20),
@fields varchar(max),
@values varchar(max)
)
as
BEGIN
DECLARE @Query NVARCHAR(max) = '';
SET @Query = ' insert into ' + QUOTENAME(@table) + ' (' + @fields + ') values(' + @values + ')';
EXEC sp_executesql @Query
end


Good luck
 
Share this answer
 
SQL
create procedure insert_record
(
@values varchar(max)
)
as
begin
declare @table table (Fields varchar(100))
insert into @table(fields) values(@values)
end



U Can't Insert Temp Value Into Another Temp Value.
 
Share this answer
 

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