简体   繁体   中英

C# Sql Incorrect syntax error

In the following command I get the exception

Incorrect syntax near 'TPA_Approved_Date'

What am I doing wrong?:

  SqlCommand sqlComm = new SqlCommand();
  sqlComm = myConnection.CreateCommand();
 sqlComm.CommandText = @"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F] 
                        SET TPA_Approver_Code='@Approver_Code' 
                        TPA_Approved_Date ='@Approved_Date'   
                        TPA_Approved_Flag='Y' WHERE 
                        MDM_Invoice_Date_Dim_Key='20150206'";
 sqlComm.Parameters.AddWithValue("@Approver_Code", Approver_Code);
 sqlComm.Parameters.AddWithValue("@Approved_Date", "2015-05-06 16:24:47.870"); 
  1. don't enclose your parameters in quotes - otherwise they will be treated as literals
  2. separate the columns with commas:

     sqlComm.CommandText = @"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F] SET TPA_Approver_Code=@Approver_Code, TPA_Approved_Date =@Approved_Date, TPA_Approved_Flag='Y' WHERE MDM_Invoice_Date_Dim_Key='20150206'"; 

Above answer is correct. But just wanted to mention, sometimes its quicker to not to use parameters (if you trust the values in variables). Like for example:

SqlCommand sqlComm = new SqlCommand();
sqlComm = myConnection.CreateCommand();
sqlComm.CommandText = @"UPDATE [MRT_MKT_BIDW].[biw].[CNX_TPA_Applied_F] 
                        SET TPA_Approver_Code='"+Approver_Code+"',
                        TPA_Approved_Date ='2015-05-06 16:24:47.870',
                        TPA_Approved_Flag='Y' WHERE 
                        MDM_Invoice_Date_Dim_Key='20150206'"; 

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