将数据插入另一个select语句的引号内的值

时间:2023-02-11 22:44:01

I am trying to insert data into multiple columns of a table with specific values. However one varchar column will have its data value coming from another table select statement placed inside a quotes.

我试图将数据插入具有特定值的表的多个列中。然而,一个varchar列将有它的数据值,来自另一个表select语句,放在引号内。

I need it placed inside without having the sub select statement interpreted as part of the string. Below is my query;

我需要它放在内部,而不需要将子select语句解释为字符串的一部分。下面是我查询;

INSERT INTO Table1
            (column1,
             column2,
             column3,
             column4)
VALUES     (484640,
            4,
            1,
            '<HTML><Head></Head><Body><a href="cgroup-histstatus.aspx?Staging_PKID=(select distinct column_1 from Table_2)">Upload results</a></Body></HTML>')

1 个解决方案

#1


6  

Currently your select query will be just treated like a string since it is enclosed within single quotes. The query will be inserted into the column, not the result.

目前,您的select查询将被当作字符串处理,因为它包含在单引号中。查询将被插入到列中,而不是结果中。

Here is the correct way:

以下是正确的方法:

INSERT INTO Table1
            (column1,
             column2,
             column3,
             column4)
SELECT DISTINCT 484640,
                4,
                1,
                '<HTML><Head></Head><Body><a href="cgroup-histstatus.aspx?Staging_PKID='
                + cast(column_1 as varchar(50))
                + '">Upload results</a></Body></HTML>'
FROM   Table_2

If you want the values from column_1 to be enclosed within single quotes then replace '+ column_1 +' with '''+ column_1 +'''.

如果希望将column_1中的值括在单引号中,那么将'+ column_1 +'替换为''+ column_1 +'。

This works even when select query returns more than one record.

即使select查询返回多个记录,它也能工作。

#1


6  

Currently your select query will be just treated like a string since it is enclosed within single quotes. The query will be inserted into the column, not the result.

目前,您的select查询将被当作字符串处理,因为它包含在单引号中。查询将被插入到列中,而不是结果中。

Here is the correct way:

以下是正确的方法:

INSERT INTO Table1
            (column1,
             column2,
             column3,
             column4)
SELECT DISTINCT 484640,
                4,
                1,
                '<HTML><Head></Head><Body><a href="cgroup-histstatus.aspx?Staging_PKID='
                + cast(column_1 as varchar(50))
                + '">Upload results</a></Body></HTML>'
FROM   Table_2

If you want the values from column_1 to be enclosed within single quotes then replace '+ column_1 +' with '''+ column_1 +'''.

如果希望将column_1中的值括在单引号中,那么将'+ column_1 +'替换为''+ column_1 +'。

This works even when select query returns more than one record.

即使select查询返回多个记录,它也能工作。