Skip to content

Commit cf870c0

Browse files
committed
- 修复 AsTreeCte + InsertInto 语法错误问题;#2045
1 parent e64fd7b commit cf870c0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

FreeSql/Internal/CommonProvider/SelectProvider/Select0ProviderReader.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,15 @@ protected string InternalGetInsertIntoToSql<TTargetEntity>(string tableName, Exp
10381038
childs.Add(new ReadAnonymousTypeInfo { DbField = dbfield, CsName = col.CsName });
10391039
}
10401040
var selectField = string.Join(", ", childs.Select(a => a.DbField));
1041+
var cteWithSql = "";
1042+
if (_is_AsTreeCte && this._select.TrimStart().StartsWith("WITH") && this._select.EndsWith("SELECT "))
1043+
{
1044+
cteWithSql = this._select.Substring(0, this._select.Length - 7);
1045+
this._select = "SELECT ";
1046+
}
10411047
var selectSql = this.ToSql(selectField);
10421048
var insertField = string.Join(", ", childs.Select(a => _commonUtils.QuoteSqlName(tb.ColumnsByCs[a.CsName].Attribute.Name)));
1043-
var sql = $"INSERT INTO {_commonUtils.QuoteSqlName(tableName)}({insertField})\r\n{selectSql}";
1049+
var sql = $"{cteWithSql}INSERT INTO {_commonUtils.QuoteSqlName(tableName)}({insertField})\r\n{selectSql}";
10441050
return sql;
10451051
}
10461052
public int InternalInsertInto<TTargetEntity>(string tableName, Expression select)

0 commit comments

Comments
 (0)