使用ASP.NET应用程序中的动态SQL插入多个值

时间:2013-09-03 10:08:01

标签: c# asp.net sql-server sql-server-2005 insert

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

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

如何使用EXEC @sql

使用dyanmic sql插入它

当我跑步时

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

我收到错误说

附近的语法不正确
  

'INSERT INTO Tablename(col1,col2,col3)SELECT'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();
如果文字列中没有空格,

Incorrect syntax error靠近with,靠近more但是。它工作正常。

为什么会这样?

2 个答案:

答案 0 :(得分: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

在输出中:

答案 1 :(得分: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

Select

之后,您遗失了Union all
  

SQL Fiddle

**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