Unix Technical Forum

pass Column Name using Parameter in SQL Statement...

This is a discussion on pass Column Name using Parameter in SQL Statement... within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I am trying to Pass Column Name(FieldName) using Parameter in SQL Statement... But i am getting error... how ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:43 PM
raghutumma@gmail.com
 
Posts: n/a
Default pass Column Name using Parameter in SQL Statement...

Hi,

I am trying to Pass Column Name(FieldName) using Parameter in SQL
Statement... But i am getting error...

how can i pass Column name using parameter???

Example:

in table i have fieldname ECountry...

Select @FName='ECountry'
Select @FName from Table...

How it works???

Thanx in Advance,
Regards,
Raghu...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:43 PM
Erland Sommarskog
 
Posts: n/a
Default Re: pass Column Name using Parameter in SQL Statement...

(raghutumma@gmail.com) writes:
> I am trying to Pass Column Name(FieldName) using Parameter in SQL
> Statement... But i am getting error...
>
> how can i pass Column name using parameter???
>
> Example:
>
> in table i have fieldname ECountry...
>
> Select @FName='ECountry'
> Select @FName from Table...
>
> How it works???


Why would you do it in the first place? Given a well-designed database,
the request does not make very much sense. But if you have a less well-
designed database, you need to do:

SELECT CASE @paramname
WHEN 'thatfield' THEN thatfield
WHEN 'thisfield' THEN thisfield
WHEN 'leftfield' THEN leftfield
END
FROM tbl


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:43 PM
--CELKO--
 
Posts: n/a
Default Re: pass Column Name using Parameter in SQL Statement...

>> how can i pass Column name using parameter? <<

You don't do this; it is bad programming. A well-designed module of
code returns a predictable result. What you have is what I call a
"Britney Spears, Squids and Automobiles" module, since it can return
anything of any data type!

Get a book on basic Software Engineering and read about coupling and
cohesion before you do any more programming in any language.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:43 PM
paulschultz54@gmail.com
 
Posts: n/a
Default Re: pass Column Name using Parameter in SQL Statement...

On Oct 19, 9:06 am, --CELKO-- <jcelko...@earthlink.net> wrote:
> >> how can i pass Column name using parameter? <<

>
> You don't do this; it is bad programming. A well-designed module of
> code returns a predictable result. What you have is what I call a
> "Britney Spears, Squids and Automobiles" module, since it can return
> anything of any data type!
>
> Get a book on basic Software Engineering and read about coupling and
> cohesion before you do any more programming in any language.


Maybe they weren't responsible for the database design, but are being
asked query from it due to business requirements or maybe it's not a
critical application and it's was easier to load a flat file into
Access than to design a proper normalized database. Regardless, it's
a legitimate question (as are your concerns about unpredictable
results), but to assume that Raghu doesn't know basic Software
Engineering is rude.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 03:43 PM
Greg D. Moore \(Strider\)
 
Posts: n/a
Default Re: pass Column Name using Parameter in SQL Statement...

<paulschultz54@gmail.com> wrote in message
news:1193191383.235885.139270@y27g2000pre.googlegr oups.com...
> On Oct 19, 9:06 am, --CELKO-- <jcelko...@earthlink.net> wrote:
>> >> how can i pass Column name using parameter? <<

>>
>> You don't do this; it is bad programming. A well-designed module of
>> code returns a predictable result. What you have is what I call a
>> "Britney Spears, Squids and Automobiles" module, since it can return
>> anything of any data type!
>>
>> Get a book on basic Software Engineering and read about coupling and
>> cohesion before you do any more programming in any language.

>
> Maybe they weren't responsible for the database design, but are being
> asked query from it due to business requirements or maybe it's not a
> critical application and it's was easier to load a flat file into
> Access than to design a proper normalized database. Regardless, it's
> a legitimate question (as are your concerns about unpredictable
> results), but to assume that Raghu doesn't know basic Software
> Engineering is rude.


You know, Joe has written several books, SQL for Smarties comes to mind.

However, one book that I doubt he'll ever be asked to write is "Joe Celko's
Guide to Winning Friends and Influencing People."

:-)


>




--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 03:43 PM
Tony Rogerson
 
Posts: n/a
Default Re: pass Column Name using Parameter in SQL Statement...

Or.... Practical SQL Solutions in the Real World.

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]


"Greg D. Moore (Strider)" <mooregr_deleteth1s@greenms.com> wrote in message
news:13hubgltianpsb8@corp.supernews.com...
> <paulschultz54@gmail.com> wrote in message
> news:1193191383.235885.139270@y27g2000pre.googlegr oups.com...
>> On Oct 19, 9:06 am, --CELKO-- <jcelko...@earthlink.net> wrote:
>>> >> how can i pass Column name using parameter? <<
>>>
>>> You don't do this; it is bad programming. A well-designed module of
>>> code returns a predictable result. What you have is what I call a
>>> "Britney Spears, Squids and Automobiles" module, since it can return
>>> anything of any data type!
>>>
>>> Get a book on basic Software Engineering and read about coupling and
>>> cohesion before you do any more programming in any language.

>>
>> Maybe they weren't responsible for the database design, but are being
>> asked query from it due to business requirements or maybe it's not a
>> critical application and it's was easier to load a flat file into
>> Access than to design a proper normalized database. Regardless, it's
>> a legitimate question (as are your concerns about unpredictable
>> results), but to assume that Raghu doesn't know basic Software
>> Engineering is rude.

>
> You know, Joe has written several books, SQL for Smarties comes to mind.
>
> However, one book that I doubt he'll ever be asked to write is "Joe
> Celko's Guide to Winning Friends and Influencing People."
>
> :-)
>
>
>>

>
>
>
> --
> Greg Moore
> SQL Server DBA Consulting Remote and Onsite available!
> Email: sql (at) greenms.com
> http://www.greenms.com/sqlserver.html
>
>


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 08:00 AM.


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