Solved

SQL WHile statement?

Posted on 2013-06-11
2
202 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert formula to max at a specific date by month 18 19
Webservices in T-SQL 3 31
Update a text value in another table 10 40
SQL Server Error 21 8 23
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

772 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