我想从单个存储过程更新许多表。 所以我动态传递表名,并从数据集中传递该表的xml架构,我必须更新但在此我运行时遇到以下错误。因为我想通过首先在xml架构中插入一个临时表来完成它,然后我将使用临时表内连接进行目标表更新的动态sql查询:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,> Addresses
-- Description: <Description,,> USE TravComSource
/* UpdateTargetDB 'Addresses' ,'<NewDataSet>
<Table>
<AddressID>65</AddressID>
<ProfileID>150</ProfileID>
<AddressType>*PRIMARY*</AddressType>
<Address>MT.ORY</Address>
<City>MAURITIUS</City>
<State>MAURITIUS</State>
<PostalCode>MOKA</PostalCode>
<Country>MAURITIUS</Country>
<UpdateStatus>true</UpdateStatus>
</Table>
<Table>
<AddressID>184</AddressID>
<ProfileID>0</ProfileID>
<AddressType>office</AddressType>
<Address>MT.ORY,</Address>
<UpdateStatus>true</UpdateStatus>
</Table>
<Table>
<AddressID>190</AddressID>
<ProfileID>0</ProfileID>
<Address>triveni </Address>
<UpdateStatus>true</UpdateStatus>
</Table>
</NewDataSet>',0 */
-- =============================================
ALTER PROCEDURE [dbo].[UpdateTargetDB]
-- Add the parameters for the stored procedure here
@TableName nvarchar(500),
@UpdateXML nvarchar(max),
@Status bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
--select * from addresses
--select * from sys.columns
--sp_columns 'addresses'
--select * from sys.columns
--create table #tempadd (AddressID numeric,ProfileID numeric,AddressType varchar(500),Address varchar (500),City varchar (500),State varchar (500),PostalCode varchar (500),Country varchar(500) ,UpdateStatus bit)
DECLARE @sql NVARCHAR(1000)
SET @sql='SELECT * into #tempadd FROM '+@TableName+ ' WHERE 1=2'
--EXEC sp_executesql @sql
EXEC sp_executesql @sql
--select * from #tempadd
declare @XMLdoc int
EXEC sp_xml_preparedocument @XMLdoc OUTPUT, @UpdateXML
--
-- select column_name,* from information_schema.columns
-- where table_name = 'addresses'
declare @selectquery nvarchar(max)
SELECT @selectquery=STUFF( -- Remove first comma
(
SELECT ', ' + column_name FROM -- create comma separated values
(
SELECT column_name FROM information_schema.columns where table_name = @TableName --Your query here
) AS T FOR XML PATH('')
)
,1,1,'')
print @selectquery
declare @xmlcolumn nvarchar(max)
SELECT @xmlcolumn=STUFF( -- Remove first comma
(
SELECT ', ' + column_name+' '+ case when DATA_TYPE='varchar' then DATA_TYPE +'(500)' else DATA_TYPE end+' ''' +column_name + '''' FROM -- create comma separated values
(
SELECT column_name,DATA_TYPE FROM information_schema.columns where table_name = @TableName --Your query here
) AS T FOR XML PATH('')
)
,1,1,'')
print @xmlcolumn
--INSERT INTO #tempadd
--SELECT @selectquery
--FROM OPENXML (@XMLdoc,'NewDataSet/Table',2)
--WITH
--(AddressID numeric 'AddressID',ProfileID numeric 'ProfileID',AddressType varchar(500) 'AddressType',Address varchar(500) 'Address',City varchar(500) 'City',State varchar(500) 'State',PostalCode varchar(500) 'PostalCode',Country varchar(500) 'Country' ,UpdateStatus bit 'UpdateStatus')
DECLARE @param NVARCHAR(50)
declare @xmlquery nvarchar(max)
SET @xmlquery='INSERT INTO #tempadd SELECT '+ @selectquery+' FROM OPENXML (@XMLDocHandle2,''NewDataSet/Table'',2) WITH ('+@xmlcolumn+')'
SELECT @param = N'@XMLDocHandle2 INT' ;
print 'error 1';
EXEC sp_executesql @xmlquery, @param, @XMLDocHandle2=@XMLdoc
EXEC sp_xml_removedocument @XMLdoc ;
print 'error 2'
select * from #tempadd
update addresses set addresses.ProfileID=#tempadd.ProfileID,
addresses.AddressType=#tempadd.AddressType,
addresses.Address=#tempadd.Address,
addresses.City=#tempadd.City,
addresses.State=#tempadd.State,
addresses.PostalCode=#tempadd.PostalCode,
addresses.Country=#tempadd.Country,
addresses.UpdateStatus=#tempadd.UpdateStatus
FROM addresses INNER JOIN #tempadd
ON addresses.AddressID=#tempadd.AddressID
drop table #tempadd
END
错误消息
AddressID, ProfileID, AddressType, Address, City, State, PostalCode, Country, UpdateStatus
AddressID numeric 'AddressID', ProfileID numeric 'ProfileID', AddressType varchar(500) 'AddressType', Address varchar(500) 'Address', City varchar(500) 'City', State varchar(500) 'State', PostalCode varchar(500) 'PostalCode', Country varchar(500) 'Country', UpdateStatus bit 'UpdateStatus'
error 1
Msg 208, Level 16, State 0, Line 1
Invalid object name '#tempadd'.
error 2
Msg 208, Level 16, State 0, Procedure UpdateTargetDB, Line 115
Invalid object name '#tempadd'.