Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-23 : 20:24:21
|
Hi brothers I want to know the datatype which is used to store the column name of table. Because I want to use that column name in my Update statementPlz help me and suggest me to do..sample Declare @colname ????? Set @colname=(SELECT c.name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE t.name='Acomp' and c.name=1) Update Acomp Set @colname=5 Where empid=101my column will be like numbers eg.,[1],[2],[3]...wherevr my input values and colname mayches I want to update the valuethis is the requirementPlz let me know is that possible...? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-23 : 20:36:31
|
you can use varchar(256) or sysname KH[spoiler]Time is always against us[/spoiler] |
|
|
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-23 : 20:41:57
|
Hi Khtan ,thanks for replyI done this.....It is not worked for meDECLARE @temp sysnameSET @temp=(SELECT c.name FROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDWHERE t.name='Acomp' and c.name=1)update ACompset @temp=5where empid=101I thnk it taking @temp as some temp variable not as column...help me out man... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-23 : 23:08:31
|
the problem is this. You can do this, you will need to use Dynamic SQLupdate ACompset @temp=5where empid=101 Read this The Curse and Blessings of Dynamic SQLWhy do you need to do this ? KH[spoiler]Time is always against us[/spoiler] |
|
|
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-23 : 23:33:02
|
The thing is...I have column..of dates from 1 to 31 and I am have timebooking by employees in rows eg., 1-Jul-2010 8 hrs2-Jul-2010 9 hrs.......Here am tryna to change from row to column.So here my column and my dates ie., 1,2,3 are identical I'll take these as reference and will update timebooking valuesto that table...understood bro...!!! If possible can you provide more links on dynamic SQL...its my kind request |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-23 : 23:47:44
|
can you post the table structure and sample data with the required output ? KH[spoiler]Time is always against us[/spoiler] |
|
|
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-24 : 00:17:52
|
The below table has the timebooked by employees data's in rowwiseEmplid Date bookedhrs100 1-Jul-2010 8100 2-Jul-2010 9100 3-Jul-2010 7and another table structure will be like thisEmplid [1] [2] [3] [4] [5] [6]......[31]I want to update the bookedhrs data's from first table to second tabl with respect to date..Using datepart I will get day..with this day am going to compare the column name of second table and gonna insert booked hrs to second table.let me know If u can't understand my wordings...! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-24 : 00:51:48
|
[code]declare @timebooked table( [Empid] int, [Date] datetime, [BookedHrs] int)insert into @timebookedselect 100, '2010-07-01', 8 union allselect 100, '2010-07-02', 9 union allselect 100, '2010-07-03', 7select [Empid], YearMth = dateadd(month, datediff(month, 0, [Date]), 0), [1] = sum(case when datepart(day, [Date]) = 1 then [BookedHrs] else 0 end), [2] = sum(case when datepart(day, [Date]) = 2 then [BookedHrs] else 0 end), [3] = sum(case when datepart(day, [Date]) = 3 then [BookedHrs] else 0 end), [4] = sum(case when datepart(day, [Date]) = 4 then [BookedHrs] else 0 end), [5] = sum(case when datepart(day, [Date]) = 5 then [BookedHrs] else 0 end),-- . . . . [31] = sum(case when datepart(day, [Date]) = 31 then [BookedHrs] else 0 end)from @timebookedgroup by [Empid], dateadd(month, datediff(month, 0, [Date]), 0)[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-24 : 01:50:33
|
Great Khtan..Thanks a lot for ur effortAccording to you...I had modified my Update command asUPDATE main SET [1]=(SELECT SUM(BookedHrs) FROM @timebooked WHERE DATEPART(DAY,[DATE])=1 GROUP BY Empid,[Date]), [2]=(SELECT SUM(BookedHrs) FROM @timebooked WHERE DATEPART(DAY,[DATE])=2 GROUP BY Empid,[Date]), [3]=(SELECT SUM(BookedHrs) FROM @timebooked WHERE DATEPART(DAY,[DATE])=3 GROUP BY Empid,[Date]) FROM AComp main JOIN @timebooked book ON main.empid=book.EmpidIs that only way we want to manually do soSET [4]= [5]= [6]= . . [31]=any other shortcuts?...can't we do keep inside while loopand increment date by comparing the column of table from Sys.columns for the table Acomp which has column 1,2,3....31 and dates of TimebookedPlz guide me khtan..!!! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-24 : 03:37:20
|
[code]update mset [1] = t.[1], [2] = t.[2], . . . from main m inner join(select [Empid], YearMth = dateadd(month, datediff(month, 0, [Date]), 0), [1] = sum(case when datepart(day, [Date]) = 1 then [BookedHrs] else 0 end), [2] = sum(case when datepart(day, [Date]) = 2 then [BookedHrs] else 0 end), [3] = sum(case when datepart(day, [Date]) = 3 then [BookedHrs] else 0 end), [4] = sum(case when datepart(day, [Date]) = 4 then [BookedHrs] else 0 end), [5] = sum(case when datepart(day, [Date]) = 5 then [BookedHrs] else 0 end),-- . . . . [31] = sum(case when datepart(day, [Date]) = 31 then [BookedHrs] else 0 end)from timebookedgroup by [Empid], dateadd(month, datediff(month, 0, [Date]), 0))t on m.Empid = t.Empid and m.[date] = t.[YearMth][/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
karthick.amace
Starting Member
23 Posts |
Posted - 2010-07-24 : 05:08:33
|
Thanks a lot man...we will end by this...Appreciate your response..Keep it up and do this service lifelong |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-07-27 : 16:05:48
|
quote: Originally posted by khtan you can use varchar(256) or sysname
FYI, SYSNAME is an NVARCHAR(128).. :) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-27 : 20:37:53
|
quote: Originally posted by Lamprey
quote: Originally posted by khtan you can use varchar(256) or sysname
FYI, SYSNAME is an NVARCHAR(128).. :)
Oh . . is it ? There must be some error with my brain, it got shifted left 1 bit EDIT : and i also got the N part wrong KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|