简体   繁体   中英

Turn dynamic SELECT query into UPDATE stored procedure SQL

I am rather new to this and I am having a hard time into converting the below SQL query into an UPDATE statement for a stored procedure.

SELECT 'select'+
stuff((
SELECT ',' + 'dbo.' + Function_Name + '(' + Parameters_List + ')'  FROM 
[SPECIFIC_DATABASE]..Specific_table c WHERE c.Table_Name = t.Table_Name FOR 
XML PATH('')),1,1,'') 
+' from [' + Database_Name +'].[dbo].['+Table_Name+'] '
+ 'Where Audit_ID>' + CAST(@Audit_ID as nvarchar(100))
As 'Specific Queries' 
FROM (SELECT Distinct Database_Name, Table_Name FROM [SPECIFIC_DATABASE]..Specific_table) t

The UPDATE query should be something like

UPDATE Table_name
SET Column_name = Function_Name(Parameters_List)
WHERE Audit_id >= @Audit_ID
FROM [SPECIFIC_DATABASE]..Specific_table

Any suggestions and guidelines on this would be much appreciated!

I think this should give you what you want, but I don't see any any reference to a Column_Name, so I'm assuming you will hardcode that.

select 'UPDATE tbl' + stuff((
            select ' set Column_Name = ' + 'dbo.' + Function_Name + '(' + Parameters_List + ')'
            from [SPECIFIC_DATABASE]..Specific_table c
            where c.Table_Name = t.Table_Name
            for xml PATH('')
            ), 1, 1, '') 
       + ' from [' + Database_Name + '].[dbo].[' + Table_Name + '] tbl' 
       + 'Where Audit_ID>' 
       + CAST(@Audit_ID as nvarchar(100)) as 'Specific Queries'
from (
    select distinct Database_Name, Table_Name
    from [SPECIFIC_DATABASE]..Specific_table
    ) t

If the answer's not right then it might be helpful if you post what is the current output of your first query and maybe some more details as to what are the contents of the table called "Specific_table" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM