2

在 SQL Server 2005 中,我们可以将多个值插入为

INSERT INTO
TableName
(col1, col2, col3)
SELECT
'text1', 'text2', 1
UNION ALL
'text3', 'text4', 2

如何使用动态 sql 插入它EXEC @sql

当我跑

EXEC '  INSERT INTO
    TableName
    (col1, col2, col3)
    SELECT
    ''text1'', ''text2'', 1 -- using double quotation marks to escape
    UNION ALL
    ''text3'', ''text4'', 2
 '

我收到错误消息

附近的语法不正确

'插入表名(col1,col2,col3)选择'text1','text2',1 UNION ALL SELECT'。

为什么它不执行?另外,为什么 sql 没有打印我输入的整个 sql 语句?为什么它在第二次选择后修剪?

谁能帮我写这个?

编辑

在我尝试了Devart's解决方案之后。它在 SQL Server 和我的 ASP.NET 应用程序中运行良好。但是当任何文本中有空格时问题就来了

EXEC ('
 INSERT INTO TableName (col1, col2, col3)
 SELECT ''text1'' AS t1, ''text2'' AS t2, 1 AS t3
 UNION ALL
 SELECT ''text3'', ''text4'', 2
')

这工作正常...

EXEC ('
 INSERT INTO TableName (col1, col2, col3)
 SELECT ''text1'' AS t1, ''text2'' AS t2, 1 AS t3
 UNION ALL
 SELECT ''text3 with space in it'', ''text4 more spaces'', 2
')

只能在 SSMS 中正常工作,但不能在 asp.net 应用程序中正常工作。

代码如下..

var cmd = new SqlCommand 
{ commandText = "myProcedure", commandType = CommandType.StoredProcedure, connection = myGlobalConnection };

cmd.Parameters.AddWithValue("@sqlParam", thatLongDynamicString);
cmd.ExecuteNonQuery();

throw 是Incorrect syntax errornearwith和 near more如果文本列中没有空格。它工作正常。

为什么会这样?

4

2 回答 2

2

试试这个——

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL =  '
 INSERT INTO TableName (col1, col2, col3)
 SELECT ''text1'' AS t1, ''text2'' AS t2, 1 AS t3
 UNION ALL
 SELECT ''text3'', ''text4'', 2'

EXEC sys.sp_executesql @SQL

或者试试这个 -

EXEC ('
 INSERT INTO TableName (col1, col2, col3)
 SELECT ''text1'' AS t1, ''text2'' AS t2, 1 AS t3
 UNION ALL
 SELECT ''text3'', ''text4'', 2
')

更新:

我认为Dynamic SQL在你的情况下使用是不好的做法。尝试使用XML从您的客户端生成并以此XML为参数执行存储过程。例如:

IF OBJECT_ID ('dbo.usp_InsertData') IS NOT NULL
   DROP PROCEDURE dbo.usp_InsertData
GO

CREATE PROCEDURE dbo.usp_InsertData
    @Data XML
AS BEGIN

    --INSERT INTO <your_table> (...)
    SELECT 
          t.c.value('@col1', 'NVARCHAR(MAX)') 
        , t.c.value('@col2', 'NVARCHAR(MAX)') 
        , t.c.value('@col3', 'INT') 
    FROM @Data.nodes('root/item') t(c)

END
GO

DECLARE @XML XML
SELECT @XML = '
<root>
    <item col1="text3" col2="text4" col3="1"/>
    <item col1="text3 with space in it" col2="text4 more spaces" col3="2"/>
</root>'

EXEC dbo.usp_InsertData @XML

在输出中:

于 2013-09-03T10:11:34.213 回答
1

你可以试试这个。这将起作用。

DECLARE @Sql VARCHAR(MAX)

SET @Sql = '
insert into temp (id,name,address) 
SELECT 1,''text1'', ''text2'' 
UNION ALL 
select 2,''text3'', ''text4'''

--PRINT @Sql
EXEC (@Sql)

SELECT *
FROM temp

你失踪SelectUnion all

SQL小提琴

**EDIT**

试试这个 C# 代码

    StringBuilder sbQuery = new StringBuilder();
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con; //Your connection string"        
    sbQuery.Append("insert into temp (id,name,address) SELECT 1,'text1', 'text2' UNION ALL select 2,'text3', 'text4'"); // your query goes here
    cmd.CommandText = "Testing1"; // your procedure name
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@SQL",sbQuery.ToString()));
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = new DataTable();
    dt = ds.Tables[0];
    GridView1.DataSource = dt;
    GridView1.DataBind();  

您的店铺流程

CREATE PROCEDURE Testing1
@SQL varchar(Max)
AS
/* SET NOCOUNT ON */        
exec (@SQL)
select * from temp
RETURN
于 2013-09-03T10:25:00.483 回答