插入另一个参数以及表类型

时间:2014-10-21 12:31:57

标签: sql tsql sql-server-2012

tblEffort有3列EmployeeName,Effort,Date。我从表类型参数中获取前2列值,并且必须为所有这些值插入公共日期。

  1. 没有选择在dtCSV中包含timesheetdate
  2. 除了在插入的列上再次运行更新之外还应该有其他方法吗?
  3. 以下代码给出错误 - 必须声明表变量" @ TimesheetDate"

    CREATE TYPE dbo.SaveEffort_TableType AS TABLE
    (
      EmployeeName varchar(200),
      Effort decimal(18,2)  
    );
    GO
    
    CREATE PROCEDURE SaveEmployeeEffort
    (     
     @TimesheetDate datetime,
     @dtCSV AS dbo.SaveEffort_TableType readonly
    )
    AS
    BEGIN
     INSERT INTO tblEffort(EmployeeName,Effort,[Date]) 
     SELECT * FROM @dtCSV,@TimesheetDate
    END
    

1 个答案:

答案 0 :(得分:1)

您需要将变量放入SELECT子句,而不是FROM子句

CREATE PROCEDURE SaveEmployeeEffort
(     
 @TimesheetDate datetime,
 @dtCSV AS dbo.SaveEffort_TableType readonly
)
AS
BEGIN
 INSERT INTO tblEffort(EmployeeName,Effort,[Date]) 
 SELECT EmployeeName,Effort, @TimesheetDate FROM @dtCSV
END