简体   繁体   中英

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. I want to fill the following table:

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

I have following query which is only filing a Value column:

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.

If you want to change your @SQL_String expression, we can achieve this the following way. 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. When you want to select a value from a table to insert it, you must use insert into .. select .. Something like that:

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

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

2) UPDATE query

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

Add static value to your SELECT query :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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