Unix Technical Forum

Passing an Array and/or Variable Field Name to an SProc

This is a discussion on Passing an Array and/or Variable Field Name to an SProc within the SQL Server forums, part of the Microsoft SQL Server category; --> I have 2 questions. I am trying to write a stored procedure to update a table. I am trying ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:02 PM
~TheIcemanCometh~
 
Posts: n/a
Default Passing an Array and/or Variable Field Name to an SProc

I have 2 questions.

I am trying to write a stored procedure to update a table. I am trying
to pass a variable that represents the name of the column/field and
another for the value that I am changing.

For example:
@FieldName VARCHAR(100)
@FieldValue VARCHAR(100)
AS
UPDATE tblTHETABLE
SET @FieldName = @FieldValue

First is it possible to use a variable as the column/field name? If
so, how do I go about it?

Also, it would be nice if I could have the @FieldName and @FieldValue
variables as arrays. Is that possible?

Thank-you for any assistance
Bill
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:02 PM
Simon Hayes
 
Posts: n/a
Default Re: Passing an Array and/or Variable Field Name to an SProc


"~TheIcemanCometh~" <bhazelwood@delta-elevator.com> wrote in message
news:8d372e43.0402171320.5d263673@posting.google.c om...
> I have 2 questions.
>
> I am trying to write a stored procedure to update a table. I am trying
> to pass a variable that represents the name of the column/field and
> another for the value that I am changing.
>
> For example:
> @FieldName VARCHAR(100)
> @FieldValue VARCHAR(100)
> AS
> UPDATE tblTHETABLE
> SET @FieldName = @FieldValue
>
> First is it possible to use a variable as the column/field name? If
> so, how do I go about it?
>
> Also, it would be nice if I could have the @FieldName and @FieldValue
> variables as arrays. Is that possible?
>
> Thank-you for any assistance
> Bill


The short answer is that it's possible, but probably not advisable. The
first link should help explain why; the second covers arrays:

http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/arrays-in-sql.html

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 08:02 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Passing an Array and/or Variable Field Name to an SProc

[posted and mailed, please reply in news]

~TheIcemanCometh~ (bhazelwood@delta-elevator.com) writes:
> I am trying to write a stored procedure to update a table. I am trying
> to pass a variable that represents the name of the column/field and
> another for the value that I am changing.
>
> For example:
> @FieldName VARCHAR(100)
> @FieldValue VARCHAR(100)
> AS
> UPDATE tblTHETABLE
> SET @FieldName = @FieldValue
>
> First is it possible to use a variable as the column/field name? If
> so, how do I go about it?
>
> Also, it would be nice if I could have the @FieldName and @FieldValue
> variables as arrays. Is that possible?


Anything is possible, but what's the point? Why not construct the
SQL statements in client code instead?

If you really want to know how to do it, I have an article on my web
site. There you also learn why you should not do it.
http://www.sommarskog.se/dynamic_sql.html.
--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com