Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Column name as a parameter

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-04-13 : 12:21:59
Scott writes "Hello,

Is it possible to pass a column name of a table through to a stored procedure? I would like to create a generic procedure that will check different values in a table based on the column name. (I know I could construct an if statement for all of the cases and hard code the column names but that isn't very elegant)

The stored procedure gets called when a user logs on to a particular webpage and should check to see if that user is allowed access. A column of type bit should be read and the result sould denote wheter a user gets access (0 = no, 1=yes). The part that doesn't work is that the @columnname variable doesn't get resolved into the able column name as I thought it would.

For example a call to the sp would look like:
admCheckAccessRights '8623648', 'AlterClientDetails'

Any suggestions would be most helpful.

Thanks,

Scott Fergusson.




Here is the stored procedure to illustrate:

CREATE PROCEDURE admCheckAccessRights
@SessionID varchar(50),
@ColumnName varchar(50),
@LoggedOn bit output
AS
declare @allowed bit

set @LoggedOn = 0

select @allowed = @ColumnName
from Staff
where SessionID=@SessionID

if @allowed = 0
begin
set @LoggedOn = 0
end
else
begin
set @LoggedOn = 1
end"

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2002-04-13 : 12:22:00
Please read the FAQ section on dynamic SQL.
Go to Top of Page
   

- Advertisement -