Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InsertOrUpdate 若字段名称为Index等关键字时语句执行失败 #816

Closed
VicBilibily opened this issue Jul 6, 2021 · 3 comments
Closed

Comments

@VicBilibily
Copy link

VicBilibily commented Jul 6, 2021

原始错误:

System.Exception: 'Index' 附近有语法错误。如果它要作为表提示的一部分,则必须有 WITH 关键字和圆括号。有关正确的语法,请参阅 SQL Server 联机丛书。
')' 附近有语法错误。
 ---> Microsoft.Data.SqlClient.SqlException (0x80131904): 'Index' 附近有语法错误。如果它要作为表提示的一部分,则必须有 WITH 关键字和圆括号。有关正确的语法,请参阅 SQL Server 联机丛书。
')' 附近有语法错误。
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
   at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at FreeSql.Internal.CommonProvider.AdoProvider.ExecuteNonQuery(DbConnection connection, DbTransaction transaction, CommandType cmdType, String cmdText, Int32 cmdTimeout, DbParameter[] cmdParms)
ClientConnectionId:a00fc104-fbc8-4edb-98bd-6a537bce648b
Error Number:1018,State:1,Class:15
   --- End of inner exception stack trace ---
   at FreeSql.Internal.CommonProvider.AdoProvider.LoggerException(IObjectPool`1 pool, PrepareCommandResult pc, Exception ex, DateTime dt, StringBuilder logtxt, Boolean isThrowException)
   at FreeSql.Internal.CommonProvider.AdoProvider.ExecuteNonQuery(DbConnection connection, DbTransaction transaction, CommandType cmdType, String cmdText, Int32 cmdTimeout, DbParameter[] cmdParms)
   at FreeSql.Internal.CommonProvider.InsertOrUpdateProvider`1.RawExecuteAffrows()
   at FreeSql.Internal.CommonProvider.InsertOrUpdateProvider`1.ExecuteAffrows()

尝试修复:VicBilibily@24f3e2a UnitTest:VicBilibily@e64247d 但是改动后需要改动较多的单元测试

数据库的具体版本

SQLServer2008R2

安装的包

FreeSql.Provider.SqlServer

.net framework/. net core? 及具体版本

NET5
@VicBilibily
Copy link
Author

我的临时解决方案
在Fsql初始化构建时把对应的那个字段处理一下

 var type = fsql.CodeFirst.GetTableByEntity(typeof(ProductionDispatchWorker));
 if (type.Columns.TryGetValue(nameof(ProductionDispatchWorker.Index), out var columnInfo))
      columnInfo.Attribute.Name = new FreeSql.SqlServer.SqlServerUtils(fsql).QuoteSqlName(columnInfo.Attribute.Name);

@2881099
Copy link
Collaborator

2881099 commented Jul 6, 2021

打印一下 SQL

@VicBilibily
Copy link
Author

VicBilibily commented Jul 8, 2021

var iou = fsql.InsertOrUpdate<tbiou05>().SetSource(new tbiou05 { id = 1, index = "01" });
var sql = iou.ToSql();

class tbiou05
{
    public int id { get; set; }
    public string index { get; set; }
}
MERGE INTO [tbiou05] t1 
USING (SELECT 1 as id, N'01' as index ) t2 ON (t1.[id] = t2.id) 
WHEN MATCHED THEN 
  update set [index] = t2.index 
WHEN NOT MATCHED THEN 
  insert ([id], [index]) 
  values (t2.id, t2.index);

t2的字段名没有括起来

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants