sql server - How do I run SQL queries on different databases dynamically? -


I have a SQL Server stored procedure that I use to backup data from our database before I upgrade, and I Actually passing as a database in the form of a database and being able to run the process stored on many databases. Is there an easy way to do this? Best of all, I can understand that SQL is dynamically created in stored function, but it seems that there is a wrong way to do this.

There is no other way to do this. Dynamic SQL is the only way; If you get strict control in the name of DB and who is running it, then you are sorting everything together, but if there is no definitive use to avoid the parameter safely:

  Purchase Process DoStuff @dbName NVARCHAR (50) DECLARE @sql NVARCHAR (1000) SET @sql = 'SELECT stuff FROM' + QUOTENAME (@dbName) + '..Technname where accessories = other content EXEC SP_ExecuteSQL (@sql)  

Obviously, if anything else is happening, then you can check any other input again Use the potentially parameterized dynamic SQL, for example:

  create processor dstf @ dbn nvrarr (50) @sam value nVHAR (10) escalator @ sql nyharr ( 1000) SET @ SXL = 'Certified feed' + quotation (@dbnm) + '..TableName ou stuff = @PytharStaffExic SP_XAQualSQL (@ SQL,' PYTHSTAFF NVHAR (10) ', @someValue)  

This again ensures that the parameters for dynamic SQL have been passed safely and the chances of injection attacks are decreasing. It also improves the probability that the execution plan associated with the query will be reused.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -