SQL Server执行INSERT时SELECT查询无法使用标量变量问题咨询
问题解答
1. 如何将itemId列的取值赋值给名为id的其他列
INSERT语句的列顺序和SELECT语句的返回列顺序是一一对应的,只需要将ts.itemId放在SELECT的第一个返回列位置,和INSERT的首列[id]对应即可,不需要额外声明变量中转。
2. 如何向指定列写入固定相同的字符串
在SELECT语句和目标列对应的位置直接写固定字符串字面量即可,比如要给[type]列写入固定值Solutions,直接在SELECT对应位置写'Solutions'即可。
3. 为什么无法调用自己声明的标量变量
SQL语法不支持在INSERT INTO ... SELECT结构的SELECT查询块中,同时执行标量变量赋值和返回插入用结果集两个操作。且你声明的两个变量没有实际使用必要,直接按上述两个方案调整即可。
修正后代码
INSERT INTO ticket_historical_actions ( [id] ,[type] ,[Solutions _rowId] ,[Solutions_dw_dateCreate] ,[Solutions_dw_dateMod] ,[Solutions_dw_dateDelete] ,[Solutions_sourceId] ,[Solutions_date_create] ,[Solutions_date_mod] ,[Solutions_date_approval] ,[Solutions_itemId] ,[Solutions_solutionTypeName] ,[Solutions_content_plainText] ,[Solutions_userId] ,[Solutions_userId_editor] ,[Solutions_userId_approval] ,[Solutions_userName] ,[Solutions_userName_approval] ,[Solutions_status] ,[Ticket_rowId] ,[Ticket_dw_dateCreate] ,[Ticket_dw_dateMod] ,[Ticket_dw_dateDelete] ,[Ticket_sourceId] ,[Ticket_date_create] ,[Ticket_date_mod] ,[Ticket_date_close] ,[Ticket_date_solve] ,[Ticket_entityId] ,[Ticket_name] ,[Ticket_date] ,[Ticket_status] ,[Ticket_is_deleted] ,[Ticket_content_PlainText] ,[Ticket_type] ,[Ticket_urgency] ,[Ticket_impact] ,[Ticket_priority] ,[Ticket_requestTypeId] ,[Ticket_userId_lastUpdater] ,[Ticket_userId_recipient] ,[Ticket_time_to_resolve] ,[Ticket_time_to_own] ,[Users_rowId] ,[Users_dw_dateCreate] ,[Users_dw_dateMod] ,[Users_dw_dateDelete] ,[Users_sourceId] ,[Users_date_create] ,[Users_date_mod] ,[Users_name] ,[Users_LastName] ,[Users_firstName] ,[Users_phone] ,[Users_mobile] ,[Users_language] ,[Users_profileId] ,[Users_entitieId] ,[Users_titleId] ,[Users_categoryId] ,[Users_managerId] ,[Company_rowId] ,[Company_dw_dateCreate] ,[Company_dw_dateMod] ,[Company_dw_dateDelete] ,[Company_sourceId] ,[Company_date_mod] ,[Company_date_create] ,[Company_completename] ,[Company_name] ,[Company_address] ,[Company_postcode] ,[Company_town] ,[Company_state] ,[Company_country] ,[Company_phonenumber] ,[Company_email] ,[Company_admin_email] ,[Company_admin_name] ) SELECT ts.itemId, 'Solutions', ts.[rowId] ,ts.[dw_dateCreate] ,ts.[dw_dateMod] ,ts.[dw_dateDelete] ,ts.[sourceId] ,ts.[date_create] ,ts.[date_mod] ,ts.[date_approval] ,ts.[itemId] ,ts.[solutionTypeName] ,ts.[content_plainText] ,ts.[userId] ,ts.[userId_editor] ,ts.[userId_approval] ,ts.[userName] ,ts.[userName_approval] ,ts.[status] ,tt.[rowId] ,tt.[dw_dateCreate] ,tt.[dw_dateMod] ,tt.[dw_dateDelete] ,tt.[sourceId] ,tt.[date_create] ,tt.[date_mod] ,tt.[date_close] ,tt.[date_solve] ,tt.[entityId] ,tt.[name] ,tt.[date] ,tt.[status] ,tt.[is_deleted] ,tt.[content_PlainText] ,tt.[type] ,tt.[urgency] ,tt.[impact] ,tt.[priority] ,tt.[requestTypeId] ,tt.[userId_lastUpdater] ,tt.[userId_recipient] ,tt.[time_to_resolve] ,tt.[time_to_own] ,tu.[rowId] ,tu.[dw_dateCreate] ,tu.[dw_dateMod] ,tu.[dw_dateDelete] ,tu.[sourceId] ,tu.[date_create] ,tu.[date_mod] ,tu.[name] ,tu.[LastName] ,tu.[firstName] ,tu.[phone] ,tu.[mobile] ,tu.[language] ,tu.[profileId] ,tu.[entitieId] ,tu.[titleId] ,tu.[categoryId] ,tu.[managerId] ,tc.[rowId] ,tc.[dw_dateCreate] ,tc.[dw_dateMod] ,tc.[dw_dateDelete] ,tc.[sourceId] ,tc.[date_mod] ,tc.[date_create] ,tc.[completename] ,tc.[name] ,tc.[address] ,tc.[postcode] ,tc.[town] ,tc.[state] ,tc.[country] ,tc.[phonenumber] ,tc.[email] ,tc.[admin_email] ,tc.[admin_name] FROM ticket_ticketSolutions ts left join ticket_tickets tt on ts.itemId =tt.sourceId left join ticket_users tu on ts.userId = tu.sourceId left join ticket_company tc on tu.entitieId = tc.sourceId
内容的提问来源于stack exchange,提问作者Jack_paul
相关产品推荐
相关产品推荐

