STORED PROC - 选择然后插入

时间:2014-08-29 20:17:05

标签: sql sql-server stored-procedures

我是编码存储过程的新手。基本上我只想将表1的前10条记录转移到表2(我创建的)。我的代码无法插入从表1中选择的记录。我不确定我是否遗漏了某些内容,或者我做错了。请帮忙。提前谢谢!

注意:

1.)表1和表2具有相同的数据类型。

2。)我需要每列的参数才能在c#.net图中调用它们

这是我的SP:

**

USE [GDW]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_BI_Get_AsiaTopTen]

AS BEGIN
  DECLARE 
  @fyname nvarchar(120),
  @fmonth nvarchar(120),
  @fmonth_num int = null,
  @world_area nvarchar(120) = null

    SELECT TOP 10 
  @fyname= [FYName], 
  @fmonth= [FMONTH], 
  @fmonth_num= [FMONTH_NUM],
  @world_area= [World_AREA]
  FROM [dbo].[tbl_BI_FastReport1_Temp]
  WHERE [World_AREA] = 'Asia'
  AND [FMONTH_NUM] = '201401'

  INSERT INTO [dbo].[tbl_BI_AsiaTopTen]
  (
  [FYName],
  [FMONTH],
  [FMONTH_NUM],
  [World_AREA]
  )
  VALUES
  (
  @fyname,
  @fmonth,
  @fmonth_num,
  @world_area
  )

  END

**

3 个答案:

答案 0 :(得分:1)

ALTER PROCEDURE [dbo].[sp_BI_Get_AsiaTopTen]
AS 
BEGIN
  SET NOCOUNT ON;

      INSERT INTO [dbo].[tbl_BI_AsiaTopTen]
                      ([FYName],[FMONTH],[FMONTH_NUM],[World_AREA])
    SELECT TOP 10 [FYName] 
                , [FMONTH] 
                , [FMONTH_NUM]
                , [World_AREA]
    FROM [dbo].[tbl_BI_FastReport1_Temp]
    WHERE [World_AREA] = 'Asia'
    AND [FMONTH_NUM] = '201401'
 -- ORDER BY SomeCOlumn 
END

答案 1 :(得分:0)

尝试这样的事情:


    INSERT INTO [dbo].[tbl_BI_AsiaTopTen]
    (
    [FYName],
    [FMONTH],
    [FMONTH_NUM],
    [World_AREA]
    )
    SELECT TOP 10 
    [FYName], 
    [FMONTH], 
    [FMONTH_NUM],
    [World_AREA]
    FROM [dbo].[tbl_BI_FastReport1_Temp]
    WHERE [World_AREA] = 'Asia'
    AND [FMONTH_NUM] = '201401'

答案 2 :(得分:0)

你不需要存储过程...我记得,你可以做到

insert into table2从table1中选择前10个col1,col2,col3,其中col1 = ....

结帐这个msdn链接......

http://msdn.microsoft.com/en-us/library/ms174335.aspx