【问题标题】:Update table in stored procedure with the new data使用新数据更新存储过程中的表
【发布时间】:2015-09-25 10:14:59
【问题描述】:

我有一个存储过程,我将 3 个参数传递给它,并将整个内容导出到此表中,我们称之为dbo.myFinalTable。我想实现两件事:

任务 #1:

当我使用传递的任何参数再次执行该过程时,我需要附加数据。

我尝试在最终表创建结束时添加它,但总行数保持不变。有什么要做的?

IF (EXISTS (SELECT * 
            FROM INFORMATION_SCHEMA.TABLES 
            WHERE TABLE_SCHEMA = 'myDatabase' 
              AND TABLE_NAME = 'dbo.myFinalTable'))
BEGIN
    INSERT INTO dbo.myFinalTable
       SELECT
           someValues 
       FROM #tempInWhichIcalulate
       ORDER BY someOtherStuff
END

任务 #2:

我是这样执行的:

execute [myStoredProcedure] 'someString', '123', '2013-03-14'

我希望上面的查询仅在idtheDate 列(来自dbo.MyFinalTable;这些列的数据从存储过程传递,在本例中为'123''2013-03-14' ) 那里没有这些值。

注意:我的过程声明如下所示:

alter procedure [myStoredProcedure]
    (@theString varchar (30),
     @id varchar (10),
     @theDate date)

【问题讨论】:

  • 您好,您可以发布dbo.FinalTable 的结果吗? @CM2K
  • 重要的列是theDateid,其余的只是一些计算出来的值。无法发布我的真实表格
  • 这是你的存储过程alter procedure [myStoredProcedure] ( @theString varchar (30), @id varchar (10), @theDate date )@CM2K
  • 但是你的存储过程中没有查询?只有变量。没有使用变量的Select statement。 @CM2K

标签: sql sql-server tsql stored-procedures sql-server-2012


【解决方案1】:

如果我理解你的目标,这可能是查询:

insert into dbo.myFinalTable
select
@theString as MyString,
@id as MyId,
@theDate as MyDate
where not exists(select * from dbo.myFinalTable 
                 where MyId = @id
                 and MyDate = @theDate)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2012-03-15
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多