sql查询在运行时创建一个表,并从数据库的select语句中插入值

问题描述:

我正在尝试做一个表(@tbl)运行时并从数​​据库中插入select语句中的数据,因为我已经完成的工作是sql查询在运行时创建一个表,并从数据库的select语句中插入值

declare @tbl TABLE (
     Item   int 
) 

begin 

insert into @tbl values select cid from tbl_custumer where cus_ph like '%'+'987'+'%' 
select * from @tbl 
end 

为“选择CID”的语句返回的多条记录

我想你可能想要的代码看起来像这样:

begin 
    declare @tbl TABLE (
      Item int 
    ); 

    insert into @tbl(Item) 
     select cid 
     from tbl_custumer 
     where cus_ph like '%'+'987'+'%'; 

    select * 
    from @tbl; 
end; 

注意事项:

  • begin/end块是不是真的有必要,但我猜你想为其他原因(存储过程,if,或类似的东西)。
  • 使用insert . . . select时,不需要values关键字。
  • 在每个SQL语句的末尾使用分号。虽然它们是可选的,但它们使代码更容易遵循。
+0

这是解决thanx先生 – sam5808