简体   繁体   English

使用@temp表变量插入SQL Server

[英]Insert into SQL Server using @temp table variable

I wanted to check if something like this is possible. 我想检查一下是否有可能这样。 I have a table variable with just Yes and No . 我有一个表变量,只有YesNo I want to create a select query using this (basically cross join) , and than use this select query to insert all the data in a table 我想使用此方法(基本上是交叉联接)创建一个选择查询,然后使用该选择查询将所有数据插入表中

DECLARE @testTable TABLE (testTable nvarchar(3))

INSERT INTO @testTable VALUES ('Yes')
INSERT INTO @testTable VALUES ('No')

INSERT INTO DimLifeEventFlags_Stage_1
(
Select 
    LET.ID AS eID,
    LERS.ID AS cID,
    DYLE.testTable AS testTable
from 
    mainTable1 LERS, 
    mainTable2 LET , 
    @testTable DYLE
    )

There no reason you can't do what you're doing but if I were writing this I would Use CROSS JOIN join to be explicit. 没有理由你不能做你正在做的事情,但是如果我正在写这篇文章,我会使用CROSS JOIN join来明确。 CROSS JOIN also makes it easier if your mixing in LEFT or INNER joins. 如果您在LEFT或INNER中进行混音,CROSS JOIN也会使操作变得更容易。

I would also not use the temp table at all and instead use the values clause 我也不会使用临时表,而是使用values子句

INSERT INTO DimLifeEventFlags_Stage_1
(
Select 
    LET.ID AS eID,
    LERS.ID AS cID,
    DYLE.testTable 
from 
    mainTable1 LERS  
    CROSS JOIN mainTable2 LET 
    CROSS JOIN (Values ('Yes'), 
                        ('No') 
                ) as DYLE (testTable )
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM