简体   繁体   English

在语句中使用 SELECT 填充多于一列

[英]Fill more than one column with SELECT in statement

I would like to ask how can I insert data into a more than one column at the same time if I have an SELECT statement in an INSERT statement.我想问一下,如果我在INSERT语句中有SELECT语句,如何同时将数据插入到一个以上的列中。 I want to fill the following table:我想填写下表:

|-------------------|-----------------|--------------|-----------------|
|     TableName     |     ColName     |     Value    |  SQL_Statement  |
|-------------------|-----------------|--------------|-----------------|

I have following query which is only filing a Value column:我有以下查询,它只提交一个 Value 列:

SET @SQL_String = 'INSERT INTO #ResultTable(Value) SELECT ' + @ColName + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' + @ColName + ' = ''' + cast(@GuidArgument AS NVARCHAR(50)) + '''';

I need to fill all the columns but I don't know how to write that query.我需要填写所有列,但我不知道如何编写该查询。 Here is some "pseudocode" of a query I need:这是我需要的查询的一些“伪代码”:

INSERT INTO TableSchema
    (
    TableName,
    ColName, 
    Value, 
    SQL_Statement
    ) 
VALUES 
    (
    @TableName, 
    @ColName, 
    [THAT LONG SELECT FROM QUERY ABOVE], 
    @SQL_Statement
    );

Please consider that I'm using a dynamic query.请考虑我使用的是动态查询。

Thank you all!谢谢你们!

PS: It wasn't so easy to summarize my request, any edits are really appreciated. PS:总结我的要求并不容易,任何编辑都非常感谢。

If you want to change your @SQL_String expression, we can achieve this the following way.如果你想改变你的@SQL_String表达式,我们可以通过以下方式实现。 I hope this is that you are expecting.我希望这是你所期待的。

SET @SQL_String=
N'INSERT INTO #ResultTable
    (
    TableName,
    ColName, 
    Value, 
    SQL_Statement
    ) 
SELECT
    '+@TableName+', 
    '+@ColName+', 
    (SELECT ' + @ColName + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' + @ColName + ' = ''' + cast(@GuidArgument AS NVARCHAR(50)) + '''),
    '''+@SQL_Statement+''''

You don't want to use the VALUES keyword here.您不想在此处使用VALUES关键字。 When you want to select a value from a table to insert it, you must use insert into .. select .. Something like that:当你想从表中选择一个值来插入它时,你必须使用insert into .. select ..类似的东西:

Insert INTO TableName 
       (col1, 
        col2, 
        col3, 
        col4)
Select 'AStaticValue', 
       'AnotherStaticValue', 
        col1, 
        col4
FROM    AnotherTable 
WHERE   SomeCondition = true

So in your case, it should look something like:所以在你的情况下,它应该是这样的:

INSERT INTO TableSchema
    (
    TableName,
    ColName, 
    Value, 
    SQL_Statement
    ) 
Select
    @TableName, 
    @ColName, 
    ColumnNameFromYourQuery, 
    @SQL_Statement
From your-table where condition;

My advice is to run two queries :我的建议是运行两个查询:

1) SELECT query 1) SELECT 查询

SET @SQL_String = 'INSERT INTO #ResultTable(Value) SELECT ' + @ColName + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' + @ColName + ' = ''' + cast(@GuidArgument AS NVARCHAR(50)) + '''';

2) UPDATE query 2) 更新查询

UPDATE TableSchema
SET  TableName=  @TableName, ColName= @ColName, SQL_Statement= @SQL_Statement
WHERE SQL_Statement=@SQL_Statement;

Add static value to your SELECT query :将静态值添加到您的SELECT查询:

INSERT INTO TableSchema
SELECT @TableName
    ,@ColName
    ,col1
    ,col2
    ,colX
FROM WhateverTable
WHERE some_column = 'some_value'

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

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