October 9, 2007 at 7:12 am
i have some issue in dynamic queries
i have a table with some month names and need to update based on current month and also the updating parameter value is also dynamic .
for eg consider below table and queries...
create table temp (a char(10),sep decimal(5,1),oct decimal(5,1))
insert into temp values ('as',10,20)
declare @sep decimal(5,1)
declare @oct decimal(5,1)
declare @val decimal(5,1)
declare @col nvarchar(3)
set @sep = 1000
set @oct = 300
set @col = SUBSTRING(CONVERT(varchar,GETDATE(),107),1,3)
select @col
DECLARE @SQL NVARCHAR(500)
select @sql = 'update temp set '+
SUBSTRING(CONVERT(varchar,GETDATE(),107),1,3) +'= '+ 'case when @col = ''Oct''then @oct else @sep end'
select @sql
-- exec sp_executesql @sql,N'@col decimal(5,1)', @col
exec sp_executesql @sql,N'@oct decimal,@col char,@sep decimal',@oct = @oct,@col=@col,@sep=@sep
-- update temp set Oct= case @col when 'Oct'then @oct else @sep end
select * from temp
my expectation was to update the oct column in table with @oct(300)
but this does not happen case when @col = ''Oct'' is not working
what could be tha alternative
October 9, 2007 at 7:44 am
I think you're confused, or at least one of us is. Your code only contains a single value, but you're muddying the issues here.
You have 3 columns, a month, and two values. I'm not sure what you're trying to do with the update. Which column do you want to update based on the month?
Don't use @oct, @sep, @nov, as the variables. These are placeholders. You wouldn't necessarily created separate variables for each month of data. Typically the variables represent something being set in a column, not a row. Rows being
Month Val
------ ----
'Sept' 300
'Oct' 10
Can you better explain what you're trying to do?
October 9, 2007 at 8:42 am
October 9, 2007 at 10:29 pm
thanx i needed a scenario which has table with columns for different months.thanx anyways..i solved it.
October 9, 2007 at 10:50 pm
Saritha... it would be really nice if you shared how you solved it. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
October 10, 2007 at 3:52 am
October 10, 2007 at 7:16 am
October 10, 2007 at 7:40 am
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply