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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 moving data using variables

Author  Topic 

OneStepAtaTime
Starting Member

1 Post

Posted - 2013-10-20 : 15:11:53
I need to move data from one table to another using variable percentages and business days.

Here is the basic idea:

Variables: varTable1, varTable2, varPercentage

Get varPercentage of rows of VarTable1 that have a date of "current business date -1" and place into varTable2.

I'm not sure how to write this in SQL, but it is very important that I get this done quickly so as a near last resort I'm asking here for assistance- any of which will be sincerely appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-21 : 06:36:33
some thing like below

INSERT INTO varTable2
SELECT TOP (@varPercentage) PERCENT *
FROM varTable1
WHERE datefield > = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1)
AND datefield < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
ORDER BY PK


PK is primary key of varTable1


If varTable1 etc are variables storing the tablenames then you need dyanmic sql
like

EXEC('INSERT INTO '+ @varTable2 +
' SELECT TOP (@varPercentage) PERCENT *
FROM ' + @varTable1 + '
WHERE datefield > = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1)
AND datefield < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
ORDER BY PK ')


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -