简体   繁体   English

SQL Server存储过程可插入所选数据

[英]SQL Server Stored Procedure to insert selected data

I am new to stored procedures to bear with me. 我不熟悉存储过程。 In a single procedure I am trying to select data from a table and insert that data into another table. 在一个过程中,我试图从一个表中选择数据并将该数据插入另一个表中。 For example here is my selection statement: 例如,这是我的选择声明:

SELECT SUM((qty_invoiced * price)) FROM table1,table2
WHERE table1.co_num = table2.co_num 
AND UPPER(table2.strIDX) = 'S' 
AND DATEPART(wk,table1.order_date) = DATEPART(wk,Getdate()) 
AND YEAR(table1.order_date) = YEAR(Getdate()) 

I then want to insert the value of the sum field into another table. 然后,我想将sum字段的值插入另一个表。

How would I go about doing that in the most efficient way with SQL Server. 我将如何使用SQL Server以最有效的方式做到这一点。 I can do it in .net code (C# , VB etc) but wanted to do it more efficiently with a stored procedure 我可以用.net代码(C#,VB等)执行此操作,但希望通过存储过程更有效地执行此操作

You can do this in several ways. 您可以通过多种方式执行此操作。 Are you expecting the query to return a single row, or multiple rows though? 您是否希望查询返回单行或多行?

If you are expecting it to return a single row, perhaps the most straight-forward way would be to save the value into some variable, like this... 如果您期望它返回单行,则最直接的方法可能是将值保存到某个变量中,如下所示...

DECLARE @mySum DECIMAL

SELECT @mySum = SUM((qty_invoiced * price)) 
FROM table1,table2
WHERE table1.co_num = table2.co_num 
AND UPPER(table2.strIDX) = 'S' 
AND DATEPART(wk,table1.order_date) = DATEPART(wk,Getdate()) 
AND YEAR(table1.order_date) = YEAR(Getdate()) 

And then you can use @mySum in your insert statement as the value to insert. 然后可以在插入语句中使用@mySum作为要插入的值。 That of course only works if you have a single value returned from the query though. 当然,仅当您从查询返回单个值时,该方法才有效。

If you want to insert multiple rows at once from the results of that query, you'd do something like this... 如果要一次从该查询的结果中插入多行,则可以执行以下操作...

INSERT INTO SomeTable
( TheSumColumn )
SELECT SUM((qty_invoiced * price)) 
FROM table1,table2
WHERE table1.co_num = table2.co_num 
AND UPPER(table2.strIDX) = 'S' 
AND DATEPART(wk,table1.order_date) = DATEPART(wk,Getdate()) 
AND YEAR(table1.order_date) = YEAR(Getdate()) 

Or you can use a SELECT INTO clause. 或者,您可以使用SELECT INTO子句。 Look at " Adding Rows by Using INSERT and SELECT " for examples of all of these. 有关所有这些示例,请参见使用INSERT和SELECT添加行 ”。

This sounds like what you really need is a view. 听起来您真正需要的是视图。

CREATE VIEW vWeeklySum
as
SELECT 
YEAR(table1.order_date) as [Year],
DATEPART(wk,table1.order_date) as [WeekNo],
SUM(qty_invoiced * price) [Total]
FROM table1
JOIN table2
ON  table1.co_num = table2.co_num 
WHERE UPPER(table2.strIDX) = 'S' 
GROUP BY YEAR(table1.order_date),
DATEPART(wk,table1.order_date)

This way if your raw data changes your view will change without you needing to do anything. 这样,如果您的原始数据发生更改,您的视图将发生更改,而无需执行任何操作。 Then you could use it like: 然后您可以像这样使用它:

SELECT [Total]
FROM vWeeklySum
WHERE [Year] = 2015
AND [WeekNo] = 5

If you still want to save it to a table (a snapshot), your stored proc would look something like: 如果您仍想将其保存到表(快照)中,则存储的过程将类似于:

CREATE PROC SaveWeek(DateTime @Date)
AS

DELETE FROM myTable
WHERE WeekNo = DATEPART(wk,@Date) 
AND [Year] = YEAR(@Date)

INSERT INTO myTable
([Year], WeekNo, [Total])
SELECT 
YEAR(@Date),
DATEPART(wk,@Date),
SUM(qty_invoiced * price)
FROM table1
JOIN table2
ON  table1.co_num = table2.co_num 
WHERE UPPER(table2.strIDX) = 'S' 
AND DATEPART(wk,table1.order_date) = DATEPART(wk,@Date) 
AND YEAR(table1.order_date) = YEAR(@Date)
GROUP BY YEAR(@Date),
DATEPART(wk,@Date)

Then you could call it with 那你可以用

DateTime @now
set @now = getdate()
exec SaveWeek @now

If you want to be efficient, you should never use functions for the SARG fields, so you should do something like this: 如果您想提高效率,则永远不要对SARG字段使用函数,因此应该执行以下操作:

INSERT INTO table3
SELECT SUM((qty_invoiced * price))
FROM table1 t1,table2 t2
WHERE t1.co_num = t2.co_num 
AND t2.strIDX in ('s','S') 
AND t1.order_date >= @startdate 
AND t1.order_date < @enddate

Calculate the correct values to the variables before the statement. 计算语句前变量的正确值。 @enddate is intentionally the next day, just in case your field is datetime. @enddate故意是第二天,以防万一您的字段是日期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM