Solved

SQL WHile statement?

Posted on 2013-06-11
2
197 Views
Last Modified: 2013-06-12
In the code below I can set the id and it runs fine

What I would like to do is feed in a comma seperated string and run the same code setting the @htid each time


DECLARE @htid INT
SET @htid = 71888


DECLARE @tsid INT
SET @tsid = (SELECT ID FROM dbo.TimeSheetFiles WHERE hoursTrackingID = @htid AND verified = 1)

--select
;with c as 
(
	select *, dup = ROW_NUMBER() over ( PARTITION by hoursTrackingID, timesheetID, hsid order by id  ) 
	FROM dbo.HoursTracking_DetailHours WHERE hoursTrackingID = @htid AND timeSheetID = @tsid
)

DELETE from c where dup > 1

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 39237554
hi,

one way is by creating attached function and call your code like this

DECLARE @htid varchar(500)
SET @htid = '71888,452,5630,4102,344'


DECLARE @tsid INT
SET @tsid = (SELECT ID FROM dbo.TimeSheetFiles WHERE hoursTrackingID IN (SELECT items FROM dbo.FN_GENERATETABLE(@htid,',') AND verified = 1)

--select
;with c as 
(
	select *, dup = ROW_NUMBER() over ( PARTITION by hoursTrackingID, timesheetID, hsid order by id  ) 
	FROM dbo.HoursTracking_DetailHours WHERE hoursTrackingID IN (SELECT items FROM dbo.FN_GENERATETABLE(@htid,',') AND timeSheetID = @tsid
)

DELETE from c where dup > 1

Open in new window

SQL-ROWTOCOLUMN.sql
0
 

Author Closing Comment

by:lrbrister
ID: 39242340
Sorry for late getback.
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now