Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a store procedure like....

SQL
create Procedure [dbo].[stp_UpdateDoctorAvailability] @Day nvarchar(20),@TimeString nvarchar(500), @DoctorId int as
begin
Update tblDoctorsAvailability set @Day=@Timestring where DoctorsId = @DoctorId
end


here "@day" is user Defined Column name its executing but its not updating the table values its showing previous values only please could anybody help me in this please.......


thank you in Advance
Posted

You can't do that directly. You need to implement dynamic SQL to do it. See the following code.

SQL
DECLARE @query NVARCHAR(1000)
set @query = 'UPDATE tblDoctorsAvailability SET ' + @Day + '=' + @Timestring + 'WHERE DoctorsId = ' + CAST(@DoctorId AS NVARCHAR(20));
EXEC(@query)


[Edit]
You need to use CAST or CONVERT[^] to convert an integer type to character type before concatenating it. I have update the query using CAST
[/Edit]
 
Share this answer
 
v3
Comments
chetan B Y 19-Jun-12 3:36am    
its giving an Error like

Msg 245, Level 16, State 1, Procedure stp_UpdateDoctorAvailability, Line 4
Conversion failed when converting the nvarchar value 'UPDATE tblDoctorsAvailability SET Monday=chetan WHERE DoctorsId = ' to data type int.
walterhevedeich 19-Jun-12 3:51am    
I have updated my answer. Take a look.
you can doit by dynamic query.
and convert @doctorid in varchar using convert command.

SQL
DECLARE @qry VARCHAR(max)
set @qry ='UPDATE tblDoctorsAvailability SET ' + @Day + '=' + @Timestring + 'WHERE DoctorsId = ' + convert(varchar(10),@DoctorId) +'
print (@qry)
EXEC(@qry)

in place of convert you can also use cast command as
SQL
DoctorsId = ' + cast(@DoctorId as varchar(10)) +'
 
Share this answer
 
v2
SQL
create Procedure [dbo].[stp_UpdateDoctorAvailability]
 @Day nvarchar(20),
 @TimeString nvarchar(500),
 @DoctorId int
 as
begin
Update tblDoctorsAvailability set Day=@day
    where DoctorsId = @DoctorId
end
 
Share this answer
 
v2

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