473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with stored procedure and tablename parameter

N E
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 ?
Jul 20 '05 #1
1 4305
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:3f***********************@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 ?

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bruce Lester | last post by:
I am trying to get the DTS Execute SQL task to run a simple sql server 2000 no-parameter stored procedure. The procedure runs without error when using SQL Analyzer but DTS Execute SQL reports...
3
by: Barry Young | last post by:
SQL Server 7.0 I have a stored procedure that builds some tables. To minimize down time (it is the backend for a website), I build the tables into temp (not #temp) tables, drop and rename the...
4
by: stjulian | last post by:
I have a stored procedure that is supposed to 1. Increment a counter in Table A via a transaction 2. Use this value as the primary key to add in an address to customers Table B (Referenced as a...
3
by: Wie | last post by:
I need help. How do I create a stored procedure with SQLCommand? I have tried following string, but it doesn't work. I am using vs.net 2003 professional now. dbCommand.CommandText = _...
15
by: Steve | last post by:
I am trying to develop a stored procedure for an existing application that has data stored in numerous tables, each with the same set of columns. The main columns are Time and Value. There are...
2
by: Hutty | last post by:
I'm trying to call a stored procedure from vb.net that's on the sql server. I would like to have the output on another form or a listbox where I can print results. I'm not getting any errors, but...
2
by: Krishbiztech | last post by:
hi all I have developed an asp.net application that handles college operations.. and i have created a stored procedure to delete the records under some constraints.. I have passed table...
2
by: PRITHA | last post by:
Hello, I have used stored procedure with parameters.Now I want to use the parameter in the select statement.Following is my code in the procedure. //////////////////////////////////// CREATE...
5
by: Emerik | last post by:
Hi all, I'm trying to construct a SP like this: <code> CREATE PROCEDURE UpdateInvoice @companyname varchar, @docno int AS BEGIN DECLARE @tablename varchar SET @tablename = ''
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.