473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Stored Procedure Sebquery as a Paramater

Ok,

This sounds dangerous (and yes I know it is)
But its in a controlled enviroment and I really need to know how to do
this.

How can I pass a Subquery for an Exist or In clause as a paramater
Something like this

CREATE procedure dbo.mytry
@funk varchar(1000)
as

Select * from Customers where exists(
@funk
)

GO

So I would execute something like so

exec mytry @funk='Select ID From Customers where ID < 100'

Any Ideas, I have tried LOTS of things but I can actually get it to
work.

I need to use it conjunction with a 3rd party product that can only
select from a Stored Procedure, and I can only pass paramaters to the
SP.

Any suggestions ?

Thanks

Chris

Jul 23 '05 #1
6 4506

"WertmanTheMad" <cw******@webchamps.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Ok,

This sounds dangerous (and yes I know it is)
But its in a controlled enviroment and I really need to know how to do
this.

How can I pass a Subquery for an Exist or In clause as a paramater
Something like this

CREATE procedure dbo.mytry
@funk varchar(1000)
as

Select * from Customers where exists(
@funk
)

GO

So I would execute something like so

exec mytry @funk='Select ID From Customers where ID < 100'

Any Ideas, I have tried LOTS of things but I can actually get it to
work.

I need to use it conjunction with a 3rd party product that can only
select from a Stored Procedure, and I can only pass paramaters to the
SP.

Any suggestions ?

Thanks

Chris


Erland's article on dynamic searching in TSQL might give you some ideas:

http://www.sommarskog.se/dyn-search.html

Simon
Jul 23 '05 #2
Great article,

Unfortunatley unless I am missing something in it, it does not show how
to do what I need to do.
From what I saw he is building dynamic SQL From case and if statements, that wont work for me as there are too many possible queries to be
passed.

One time it might be Select * from Customers, another Select Sum(Sale)
as Total_sales from orders.

I wont know in advance what table or data, and UNFORTUNATLEY, it need
to be executed through an SP.

Im half thinking about CREATING the SP At run time.

Chris
Simon Hayes wrote: "WertmanTheMad" <cw******@webchamps.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Ok,

This sounds dangerous (and yes I know it is)
But its in a controlled enviroment and I really need to know how to do this.

How can I pass a Subquery for an Exist or In clause as a paramater
Something like this

CREATE procedure dbo.mytry
@funk varchar(1000)
as

Select * from Customers where exists(
@funk
)

GO

So I would execute something like so

exec mytry @funk='Select ID From Customers where ID < 100'

Any Ideas, I have tried LOTS of things but I can actually get it to
work.

I need to use it conjunction with a 3rd party product that can only
select from a Stored Procedure, and I can only pass paramaters to the SP.

Any suggestions ?

Thanks

Chris

Erland's article on dynamic searching in TSQL might give you some

ideas:
http://www.sommarskog.se/dyn-search.html

Simon


Jul 23 '05 #3
Try the following:

CREATE procedure dbo.mytry
@funk varchar(1000)
as
EXEC ('Select * from Northwind.dbo.Customers where exists('+@funk+')')

GO

Yury Jhol
"WertmanTheMad" <cw******@webchamps.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Ok,

This sounds dangerous (and yes I know it is)
But its in a controlled enviroment and I really need to know how to do
this.

How can I pass a Subquery for an Exist or In clause as a paramater
Something like this

CREATE procedure dbo.mytry
@funk varchar(1000)
as

Select * from Customers where exists(
@funk
)

GO

So I would execute something like so

exec mytry @funk='Select ID From Customers where ID < 100'

Any Ideas, I have tried LOTS of things but I can actually get it to
work.

I need to use it conjunction with a 3rd party product that can only
select from a Stored Procedure, and I can only pass paramaters to the
SP.

Any suggestions ?

Thanks

Chris

Jul 23 '05 #4
Thanks,

I also got it in the other one, I just wasnt seeing it (In the
http://www.sommarskog.se/dyn-search.html arcticle that is)

Many thanks, this simple problem just opened me up to a whole new and
nearly unlimited way of using an already great product, (the 3rd part
one that is :)

CREATE procedure dbo.mytry
@funk nvarchar(4000)
as

print @funk

exec sp_executesql @sql = @funk

GO

Jul 23 '05 #5
WertmanTheMad (cw******@webchamps.com) writes:
I also got it in the other one, I just wasnt seeing it (In the
http://www.sommarskog.se/dyn-search.html arcticle that is)

Many thanks, this simple problem just opened me up to a whole new and
nearly unlimited way of using an already great product, (the 3rd part
one that is :)

CREATE procedure dbo.mytry
@funk nvarchar(4000)
as

print @funk

exec sp_executesql @sql = @funk


Now, if you instead read http://www.sommarskog.se/dynamic_sql.html,
you can see why this is a pretty useless stored procedure.

Yeah, I know that you said that your 3rd party product required you
to use stored procedure, but in that case you can call sp_executesql
directly. And then you can use parameters to it, so that you don't
expose yourself for SQL injection.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
Sort of

I could call it directly but a Line I omitted was a comment of the
actual fields I need returned.

While in design mode of the thrid party product it REQUIRES (as far as
I can tell) a valid dataset to be selected from the SP,

So if the param is blank I do something like
Select ID, FNAME, LNAME FROM CUSTOMERS WHERE SALE_AMT > 1000
Otherwise I exec the param

This way I can actually work with it in design mode.

I am actually parsing the SQL Out for injection and beyond that the
ONLY route to get it in there is behind the scenes.

The product ?
Reporting Services, As long as my query returns ID, FNAME, LNAME in it
I can pass ANY dataset to it I want from my Asp.net application.

I understand than in RS2005 this will be an available option (passing
it a custom select/dataset, but I need it now.

This works slick as can be.

And since my SQL is "Generated" from a Query Builder, the client at no
time has the ability to enter any ad-hoc sql to bang it up, the SQL is
checked for validity before it even get to RS

Thanks as Always

Chris
Erland Sommarskog wrote:
WertmanTheMad (cw******@webchamps.com) writes:
I also got it in the other one, I just wasnt seeing it (In the
http://www.sommarskog.se/dyn-search.html arcticle that is)

Many thanks, this simple problem just opened me up to a whole new and nearly unlimited way of using an already great product, (the 3rd part one that is :)

CREATE procedure dbo.mytry
@funk nvarchar(4000)
as

print @funk

exec sp_executesql @sql = @funk


Now, if you instead read http://www.sommarskog.se/dynamic_sql.html,
you can see why this is a pretty useless stored procedure.

Yeah, I know that you said that your 3rd party product required you
to use stored procedure, but in that case you can call sp_executesql
directly. And then you can use parameters to it, so that you don't
expose yourself for SQL injection.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Jul 23 '05 #7

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

Similar topics

10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
6
by: Rod Snyder | last post by:
I'm trying to set up a page with an asp.net link button that would send a user to a certain page and on page load execute a specific stored procedure tied to the button on the previous page. The...
1
by: David Hearn | last post by:
I have a SQLDataSource control on one of my web forms. The stored procedure that I am connecting to has two parameters that need to be passed in and one that is returned. One of the parameters I am...
3
by: Bonzol | last post by:
Thanx to the help of other memebers on this group i've gotten to where I am however, I seem unable to add the required paramater to my stored procedure my code Dim toreturn As...
2
by: KaizerV3 | last post by:
Hello, I made a stored procedure where u have to give the date in a paramater and then the result should come. But somehow it doesnt work. It works likes this: CREATE PROCEDURE . AS
1
by: mktilu | last post by:
i want a stored procedure example where it returns both cursor and recordcount as a paramater .
5
by: Dennis | last post by:
Hi I'm trying to alter my stored procedure to take a parameter for the Database Name, but as usual the syntax is killing me. Thanks for any help Dennis ...
3
by: tholling | last post by:
how do you drop, create and load a table in a stored procedure that has one passed paramater as input?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.