Sql Update Query - 使用动态列名更新数据,使用Temp表的表名进行批量插入 [英] Sql Update Query - Update data with dynamic column name, table name using Temp table to bulk insert

查看:68
本文介绍了Sql Update Query - 使用动态列名更新数据,使用Temp表的表名进行批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

create table #tempTable 
 (
Id varchar(200),
 Name  nchar(100) 
 
 ) 
	DECLARE @bulkinsert NVARCHAR(2000)
declare @paths nvarchar(3000) 
Set @paths ='D:\ftp\PAYH\MedicationTypeInfo.csv'
SET @bulkinsert = 
       N'BULK INSERT #tempTable FROM ''' + 
       @paths + 
       N''' WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')'

EXEC sp_executesql @bulkinsert	

ALTER TABLE #tempTable	
	ADD IDs INT IDENTITY(1,1)
			
	exec('delete  from #tempTable where #tempTable.IDs<2')

	ALTER TABLE #tempTable
  ALTER COLUMN Id int
	
	
	select * from #tempTable

declare @sql nvarchar (max);

set @sql ='update MedicalListMedicationType set MedicationType =  #tempTable.Name WHERE MedicationType_ID = #tempTable.Id left outer join #tempTable on #tempTable.Id= MedicationType_ID'


  print (@sql)
exec  (@sql)



查询工作正常选择查询。但我正在执行这个商店程序。它显示错误消息

消息156,级别15,状态1,行1

关键字''左''附近的语法不正确。



我没有遇到问题..请帮帮我。

谢谢


Query is working fine till select query. but i am executing this store procedure . it shows error message
"Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword ''left''."

I Did''t get the problem .. please help me out .
Thanks

推荐答案

更改

change
set @sql ='update MedicalListMedicationType set MedicationType =  #tempTable.Name WHERE MedicationType_ID = #tempTable.Id left outer join #tempTable on #tempTable.Id= MedicationType_ID'



with


with

set @sql ='update mt set mt.MedicationType =  #tempTable.Name
from MedicalListMedicationType mt
left outer join #tempTable on #tempTable.Id= mt.MedicationType_ID'



快乐编码!

:)


Happy Coding!
:)


这篇关于Sql Update Query - 使用动态列名更新数据,使用Temp表的表名进行批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆