如何将“ concat(col1 +'-'+ col2)”插入数据库

时间:2019-09-15 09:01:26

标签: c# asp.net sql-server-2012

我想在数据库中插入一条记录,但是由于这个'-'

INSERT INTO tblTransactionLogs (data, page, insertDate) 
    VALUES ('SELECT id, CONCAT(newcode, '-', shadeno) AS clothcode, CAST(ISNULL(T_Mtrs, 0) AS DECIMAL(10, 2)) - CAST(ISNULL(Used, 0) AS DECIMAL(10, 2)) AS rem, rate FROM tbl_cloth ORDER BY clothcode', '--', '2019-09-15')

我收到此错误:

  

消息402,第16级,状态1,第1行
  减运算符中的数据类型varchar和varchar不兼容

2 个答案:

答案 0 :(得分:1)

好像您正在尝试将SQL语句插入DECLARE @startDate DATETIME = '9/1/2019' DECLARE @endDate DATETIME = '12/31/2019' DECLARE @dayOfWeek INT = 2; DECLARE @T TABLE(ADate DATE); WITH cte_Recursion AS ( SELECT @startDate AS [Date] UNION ALL SELECT DATEADD(DAY, 1, [Date]) FROM cte_Recursion WHERE [Date] < @endDate ) INSERT INTO @T SELECT [Date] FROM cte_Recursion WHERE DATEPART(WEEKDAY, [Date]) = @dayOfWeek OPTION (MAXRECURSION 0); SELECT * FROM ( SELECT [sid], [ecode], [scty] ,[sonm] FROM [Z_ALI].[dbo].[M_STORE] WHERE [sonm] = 'PENZKI STORE' ) T CROSS JOIN @T TT; 列中。

在SQL Server中尝试将data字符用作字符串的一部分时,您需要通过将其相乘来对其进行转义:

'

但是,请注意,如果这些SQL语句是由用户输入创建的(称为second order SQL Injection),则将SQL语句存储在表中以供以后执行可能会带来安全风险。

答案 1 :(得分:0)

您应使用以下结构:

插入tblTransactionLogs(数据,页面,insertDate) 从tblY中选择x,y,z