SQL Server stored procedures - update column based on variable name..?

Posted by ClarkeyBoy on Stack Overflow See other posts from Stack Overflow or by ClarkeyBoy
Published on 2010-05-24T11:11:31Z Indexed on 2010/05/24 18:31 UTC
Read the original article Hit count: 396

Filed under:
|
|
|
|

Hi,

I have a data driven site with many stored procedures. What I want to eventually be able to do is to say something like:

For Each @variable in sproc inputs
    UPDATE @TableName SET @variable.toString = @variable
Next

I would like it to be able to accept any number of arguments.

It will basically loop through all of the inputs and update the column with the name of the variable with the value of the variable - for example column "Name" would be updated with the value of @Name. I would like to basically have one stored procedure for updating and one for creating. However to do this I will need to be able to convert the actual name of a variable, not the value, to a string.

Question 1: Is it possible to do this in T-SQL, and if so how?

Question 2: Are there any major drawbacks to using something like this (like performance or CPU usage)?

I know if a value is not valid then it will only prevent the update involving that variable and any subsequent ones, but all the data is validated in the vb.net code anyway so will always be valid on submitting to the database, and I will ensure that only variables where the column exists are able to be submitted.

Many thanks in advance,

Regards,

Richard Clarke

Edit:

I know about using SQL strings and the risk of SQL injection attacks - I studied this a bit in my dissertation a few weeks ago.

Basically the website uses an object oriented architecture. There are many classes - for example Product - which have many "Attributes" (I created my own class called Attribute, which has properties such as DataField, Name and Value where DataField is used to get or update data, Name is displayed on the administration frontend when creating or updating a Product and the Value, which may be displayed on the customer frontend, is set by the administrator. DataField is the field I will be using in the "UPDATE Blah SET @Field = @Value".

I know this is probably confusing but its really complicated to explain - I have a really good understanding of the entire system in my head but I cant put it into words easily.

Basically the structure is set up such that no user will be able to change the value of DataField or Name, but they can change Value. I think if I were to use dynamic parameterised SQL strings there will therefore be no risk of SQL injection attacks.

I mean basically loop through all the attributes so that it ends up like:

UPDATE Products SET [Name] = '@Name', Description = '@Description', Display = @Display

Then loop through all the attributes again and add the parameter values - this will have the same effect as using stored procedures, right??

I dont mind adding to the page load time since this is mainly going to affect the administration frontend, and will marginly affect the customer frontend.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server