Unix Technical Forum

problem with stored procedure and tablename parameter

This is a discussion on problem with stored procedure and tablename parameter within the SQL Server forums, part of the Microsoft SQL Server category; --> hello I am trying to do a procedure that : 1 - Take a table name, a field name ...


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 02-28-2008, 06:27 PM
N E
 
Posts: n/a
Default problem with stored procedure and tablename parameter

hello

I am trying to do a procedure that :
1 - Take a table name, a field name and a value as parameter
2 - check if the fieldname=value in the tablename table
3 - returns 1 if so, 0 if not

A -
I tried :

drop procedure checkmytable
GO

create procedure checkmytable
@p_table nvarchar(50),
@p_field nvarchar(50),
@p_value nvarchar(50)
as
if exists ( select * from @p_table where @p_field = @p_value)
return 1
else
return 0

declare
@res int

exec @res=checkmytable 'mytable' , 'myfield' , '456123'
print @res


in this case, I always get the error : 'must declare @p_table'

B -
I tried

drop procedure checkmytable
GO

create procedure checkmytable
@p_table nvarchar(50),
@p_field nvarchar(50),
@p_value nvarchar(50)
as

return exec( 'if exists (select * from' + @p_table + ' where ' + @p_field +
' = ' + @p_value + ') return 1 else return 0' )
GO

declare
@res int

exec @res=checkmytable 'AM_ASSET' , 'ASSET_ID' , '18705'
print @res


in this case, I always get 0



any help ?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:27 PM
John Bell
 
Posts: n/a
Default Re: problem with stored procedure and tablename parameter

Hi

Read http://www.algonet.se/~sommar/dynamic_sql.html and the other
interesting articles on Erlands site.

John

"N E" <to@to.com> wrote in message
news:3f38f8cf$0$16518$626a54ce@news.free.fr...
> hello
>
> I am trying to do a procedure that :
> 1 - Take a table name, a field name and a value as parameter
> 2 - check if the fieldname=value in the tablename table
> 3 - returns 1 if so, 0 if not
>
> A -
> I tried :
>
> drop procedure checkmytable
> GO
>
> create procedure checkmytable
> @p_table nvarchar(50),
> @p_field nvarchar(50),
> @p_value nvarchar(50)
> as
> if exists ( select * from @p_table where @p_field = @p_value)
> return 1
> else
> return 0
>
> declare
> @res int
>
> exec @res=checkmytable 'mytable' , 'myfield' , '456123'
> print @res
>
>
> in this case, I always get the error : 'must declare @p_table'
>
> B -
> I tried
>
> drop procedure checkmytable
> GO
>
> create procedure checkmytable
> @p_table nvarchar(50),
> @p_field nvarchar(50),
> @p_value nvarchar(50)
> as
>
> return exec( 'if exists (select * from' + @p_table + ' where ' + @p_field

+
> ' = ' + @p_value + ') return 1 else return 0' )
> GO
>
> declare
> @res int
>
> exec @res=checkmytable 'AM_ASSET' , 'ASSET_ID' , '18705'
> print @res
>
>
> in this case, I always get 0
>
>
>
> any help ?
>
>



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 01:21 PM.


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