Discussion:
Stored Procedure with Column
(too old to reply)
jp2msft
2008-08-08 19:57:02 UTC
Permalink
Can a stored procedure be written with a column name?

For Table1 with ColA, ColB, ColC, I want a query like:

EXEC sp_Mine 'ColA', '100%'

I want the results to be like:

SELECT ColA, ColB, ColC
FROM Table1
WHERE ColA Like '100%'

If this is the wrong board, let me know and I'll post elsewhere. I am not an
SQL guy.
Ricardo Junquera
2008-08-11 07:17:02 UTC
Permalink
Hello

Try with Dinamic SLQ

CREATE PROCEDURE [dbo].[pr_value]
@FIELD varchar(10),
@VALUE varchar(50)
AS
DECLARE @Consulta varchar(2000)


Set @Consulta=
'SELECT ColA, ColB, ColC
FROM Table1
WHERE ' + @FIELD + ' Like ''' + @VALUE + ''''
exec(@Consulta)
--
Ricardo Junquera
Consultor Business Intelligence

BG&S Online Consultores
Ganadora del Premio Microsoft Business Awards 2008.
Partner de Soluciones : Satisfacción de Cliente.
Post by jp2msft
Can a stored procedure be written with a column name?
EXEC sp_Mine 'ColA', '100%'
SELECT ColA, ColB, ColC
FROM Table1
WHERE ColA Like '100%'
If this is the wrong board, let me know and I'll post elsewhere. I am not an
SQL guy.
jp2msft
2008-08-11 17:27:06 UTC
Permalink
Ouch!

Seems simple, but I can complicate it with another question: Is there a way
to make Dynamic SQL work with a custom DataTable that is already loaded into
a DataSet?

My custom DataTable is already a conglomeration of 6 different tables from
our database.
Post by Ricardo Junquera
Hello
Try with Dinamic SLQ
CREATE PROCEDURE [dbo].[pr_value]
@FIELD varchar(10),
@VALUE varchar(50)
AS
'SELECT ColA, ColB, ColC
FROM Table1
--
Ricardo Junquera
Consultor Business Intelligence
BG&S Online Consultores
Ganadora del Premio Microsoft Business Awards 2008.
Partner de Soluciones : Satisfacción de Cliente.
Post by jp2msft
Can a stored procedure be written with a column name?
EXEC sp_Mine 'ColA', '100%'
SELECT ColA, ColB, ColC
FROM Table1
WHERE ColA Like '100%'
If this is the wrong board, let me know and I'll post elsewhere. I am not an
SQL guy.
Loading...