Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql选择子查询_Sql_Sql Server 2005 - Fatal编程技术网

sql选择子查询

sql选择子查询,sql,sql-server-2005,Sql,Sql Server 2005,我正在系统之间进行数据转换,并准备了一个select语句,该语句标识了从table1中提取的必要行,并连接到table2,以显示一对支持列。此select语句还将空白列放入结果中,以便格式化上载到目标系统的结果 除了这个查询之外,我还需要更新一些列值,这是我希望在新表中的单独语句操作中执行的。因此,我感兴趣的是将上面的select语句作为select INTO中的子查询运行,该子查询本质上将把结果放到一个暂存表中 SELECT dbo_tblPatCountryApplication.

我正在系统之间进行数据转换,并准备了一个select语句,该语句标识了从
table1
中提取的必要行,并连接到
table2
,以显示一对支持列。此select语句还将空白列放入结果中,以便格式化上载到目标系统的结果

除了这个查询之外,我还需要更新一些列值,这是我希望在新表中的单独语句操作中执行的。因此,我感兴趣的是将上面的select语句作为
select INTO
中的子查询运行,该子查询本质上将把结果放到一个暂存表中

SELECT 
    dbo_tblPatCountryApplication.AppId, '', 
    dbo_tblPatCountryApplication.InvId,
    'Add', dbo_tblpatinvention.disclosurestatus, ...
FROM 
    dbo_tblPatInvention 
INNER JOIN 
    dbo_tblPatCountryApplication ON dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId
ORDER BY 
    dbo_tblpatcountryapplication.invid;
我希望执行上述语句,以便将结果转储到新表中。任何人都可以建议如何将语句嵌入子查询中,以便在中使用
选择功能?

试试看

INSERT INTO stagingtable (AppId, ...)
SELECT ... --your select goes here

如果暂存表不存在,并且您希望在insert上创建它,请尝试以下操作:

SELECT dbo_tblPatCountryApplication.AppId,'', dbo_tblPatCountryApplication.InvId,
       'Add', dbo_tblpatinvention.disclosurestatus .......
INTO StagingTable
FROM dbo_tblPatInvention 
INNER JOIN dbo_tblPatCountryApplication 
              ON dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId;
如果要按特定顺序插入它们,请在from子句中使用子查询:

SELECT *
INTO StagingTable
FROM 
(
   SELECT dbo_tblPatCountryApplication.AppId, '', dbo_tblPatCountryApplication.InvId, 
          'Add', dbo_tblpatinvention.disclosurestatus .......
   FROM dbo_tblPatInvention 
   INNER JOIN dbo_tblPatCountryApplication ON 
              dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId
    order by dbo_tblpatcountryapplication.invid
) a;
只需在现有查询中添加一个子句,即可创建一个新表,其中包含查询结果:

SELECT ...
INTO MyNewStagingTable -- Creates a new table with the results of this query
FROM MyOtherTable
    JOIN ...
但是,您必须确保每列都有一个名称,如中所示:

SELECT dbo_tblPatCountryApplication.AppId, -- Cool, already has a name
    '' AS Column2, -- Given a name to create that new table with select...into
    ...
INTO MyNewStagingTable
FROM dbo_tblPatInvention INNER JOIN ...
此外,您可能也希望使用,使代码更具可读性

SELECT a.AppId,
    '' AS Column2,
    ...
INTO MyNewStagingTable
FROM dbo_tblPatInvention AS i
    INNER JOIN dbo_tblPatCountryApplication AS a ON i.InvId = a.InvId
ORDER BY a.InvId

最后一点需要注意的是,将表命名为
dbo_tblXXX
看起来很奇怪,因为dbo通常是模式名,并用点符号与表名分开,例如
dbo.tblXXX
。我假设在添加into子句之前,您已经有了一个完全有效的select查询。有些人还认为在数据库中使用匈牙利符号(TBLNEY)是一种避免反模式的方式。

你知道SQL中的插入是什么吗?