【问题标题】:SQL - Table Type / passing a table as a paramaterSQL - 表类型/将表作为参数传递
【发布时间】:2015-09-09 22:29:19
【问题描述】:

我有一个将表作为参数的存储过程。

例如:我有一个类型PartsImport

CREATE TYPE PartsImport AS TABLE
(
     Number_Key varchar(10), 
     LogDate smalldatetime, 
     FullName varchar(125),  
     Descrip varchar(250)
 );

然后存储过程接受这个参数:

@PTable PartsImport ReadOnly

存储过程通过简单的select * from @PTable 向表中插入,但现在我需要在创建选择语句时将此表变量与其他表连接,但我总是收到错误消息

必须声明标量变量“@PPTable”

如何从这个@PTable 中选择一个单独的列?我试过@PTable.LogDate,但它不喜欢它的语法。是否可以在连接中使用变量表并选择列,还是仅适用于select *

【问题讨论】:

  • 你能告诉我们实际使用的查询吗? @PPTable 是什么?您是否尝试过使用别名,例如 FROM @PTable as P where P.LogDate...
  • 谢谢!!别名有效。

标签: sql sql-server-2008


【解决方案1】:

您可以尝试使用别名来引用您的表,而不是变量名。例如:

SELECT P.LogDate FROM @PTable AS P

【讨论】:

    【解决方案2】:

    试试下面的例子,当你只从表变量中获取列时工作正常,但是当你在连接中使用时,你必须使用别名和表变量名。

    CREATE TYPE PartsImport AS TABLE
    (
         Number_Key varchar(10), 
         LogDate smalldatetime, 
         FullName varchar(125),  
         Descrip varchar(250)
     );
    
     create table PartsImportother (col1 varchar(10), col2 varchar(3))
     insert into PartsImportother values('1','ads')
    
    
     Declare @table PartsImport
     insert into @table(Number_Key,LogDate,FullName,Descrip) values('1','01-01-2015','aaa','adsfadfa')
    
    -- select [specific column] from [only table variable]
      select Number_Key from @table
    
    -- select [specific column] from [table variable with join]
        select T.Number_Key from @table as T inner join  PartsImportother on T.Number_Key = PartsImportother.col1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2019-02-27
      相关资源
      最近更新 更多