Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Select only some records or everything in a stored proc.

 
   Database Help (Home) -> Programming RSS
Next:  Access 97 executables and other versions of Acces..  
Author Message
Mr.Magic

External


Since: Oct 29, 2009
Posts: 1



(Msg. 1) Posted: Thu Oct 29, 2009 12:59 pm
Post subject: Select only some records or everything in a stored proc.
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I've got a stored proc that create a temporary table from a select with a
join of multiple tables. Depending on parameters, I may want either all of
the records or just some of them. Currently what I do is create the table
with every row in it and then delete any records I don't want.

Is there a better way to do it? I'd ideally like to have a single SQL
statement so that if I change the query, I only need to do it once.

I've also thought about having a check something like:
if ( limit )
select * into #table1
from....
where x = @passedin
else
select * into #table1
from ....

The problem is SQL complains that the table already exists when you try and
create the procedure.

So anybody have any suggestions?

TIA - Jeff.

 >> Stay informed about: Select only some records or everything in a stored proc. 
Back to top
Login to vote
Russell Fields

External


Since: Jan 10, 2008
Posts: 608



(Msg. 2) Posted: Thu Oct 29, 2009 1:21 pm
Post subject: Re: Select only some records or everything in a stored proc. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Your IF SELECT * / ELSE SELECT * is interpreted by the parser as two (2)
create statements for the same table name, which, of course, is not allowed.
(Yes, I understand that only one can be created in this case, but the parser
makes a single pass (so I am told) so that is what it sees.)

You can create the temp table first, then in the IF / ELSE blocks you can do
INSERT INTO / SELECT FROM, which will cause no problem with duplicate table
definitions.

It also seems like what you are after is essentially a dynamic search
condition. If so, the strategies outlined here may help you.
http://www.sommarskog.se/dyn-search-2005.html

RLF


"Mr.Magic" wrote in message

> I've got a stored proc that create a temporary table from a select with a
> join of multiple tables. Depending on parameters, I may want either all of
> the records or just some of them. Currently what I do is create the table
> with every row in it and then delete any records I don't want.
>
> Is there a better way to do it? I'd ideally like to have a single SQL
> statement so that if I change the query, I only need to do it once.
>
> I've also thought about having a check something like:
> if ( limit )
> select * into #table1
> from....
> where x = @passedin
> else
> select * into #table1
> from ....
>
> The problem is SQL complains that the table already exists when you try
> and create the procedure.
>
> So anybody have any suggestions?
>
> TIA - Jeff.
>
>
>
>

 >> Stay informed about: Select only some records or everything in a stored proc. 
Back to top
Login to vote
sekharsql

External


Since: Oct 29, 2009
Posts: 1



(Msg. 3) Posted: Thu Oct 29, 2009 1:30 pm
Post subject: RE: Select only some records or everything in a stored proc. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

send the query


thanks,
--
www.calsql.com (my blog site)


"Mr.Magic" wrote:

> I've got a stored proc that create a temporary table from a select with a
> join of multiple tables. Depending on parameters, I may want either all of
> the records or just some of them. Currently what I do is create the table
> with every row in it and then delete any records I don't want.
>
> Is there a better way to do it? I'd ideally like to have a single SQL
> statement so that if I change the query, I only need to do it once.
>
> I've also thought about having a check something like:
> if ( limit )
> select * into #table1
> from....
> where x = @passedin
> else
> select * into #table1
> from ....
>
> The problem is SQL complains that the table already exists when you try and
> create the procedure.
>
> So anybody have any suggestions?
>
> TIA - Jeff.
>
>
>
>
> .
>
 >> Stay informed about: Select only some records or everything in a stored proc. 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 4) Posted: Thu Oct 29, 2009 5:33 pm
Post subject: Re: Select only some records or everything in a stored proc. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> I've got a stored proc that create a temporary table from a SELECT with a join of multiple tables.<,

Just like a scratch tape in a non-RDBMS file system. We don't do
things this way in SQL.

>> Depending on parameters, I may want either all of The records [sic: rows are not records] or just some of them.

CREATE PROCEDURE Vagueness (@in_parm INTEGER)
AS
SELECT X.* -- never use * in production code
FROM (<<table expression>>) AS X
WHERE x = COALESCE (@in_parm, x); -- assumes x is not null

Pass a NULL in the input parm and you get everything.
 >> Stay informed about: Select only some records or everything in a stored proc. 
Back to top
Login to vote
amish

External


Since: Apr 19, 2007
Posts: 45



(Msg. 5) Posted: Fri Oct 30, 2009 12:09 am
Post subject: Re: Select only some records or everything in a stored proc. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Oct 29, 10:21 pm, "Russell Fields"
wrote:
> Your IF  SELECT * / ELSE SELECT *  is interpreted by the parser as two (2)
> create statements for the same table name, which, of course, is not allowed.
> (Yes, I understand that only one can be created in this case, but the parser
> makes a single pass (so I am told) so that is what it sees.)
>
> You can create the temp table first, then in the IF / ELSE blocks you can do
> INSERT INTO / SELECT FROM, which will cause no problem with duplicate table
> definitions.
>
> It also seems like what you are after is essentially a dynamic search
> condition.  If so, the strategies outlined here may help you.http://www..sommarskog.se/dyn-search-2005.html
>
> RLF
>
> "Mr.Magic" wrote in message
>
>
>
> > I've got a stored proc that create a temporary table from a select with a
> > join of multiple tables. Depending on parameters, I may want either all of
> > the records or just some of them. Currently what I do is create the table
> > with every row in it and then delete any records I don't want.
>
> > Is there a better way to do it? I'd ideally like to have a single SQL
> > statement so that if I change the query, I only need to do it once.
>
> > I've also thought about having a check something like:
> >    if ( limit )
> >        select * into #table1
> >            from....
> >            where x = @passedin
> >    else
> >        select * into #table1
> >            from ....
>
> > The problem is SQL complains that the table already exists when you try
> > and create the procedure.
>
> > So anybody have any suggestions?
>
> > TIA - Jeff.


SELECT * INTO #temp1
FROM sys.databases
WHERE database_id = CASE
WHEN @id IS NOT NULL THEN @id
ELSE database_id
END

or

SELECT * INTO #temp1
FROM sys.databases
WHERE database_id = coalesce (@id, database_id)

Amish Shah
http://shahamishm.blogspot.com
 >> Stay informed about: Select only some records or everything in a stored proc. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select query in stored proc - Hi All, I think what am trying to do is quite basic. I have 3 paramaters@value1, @value2,@value3 being passed into a stored proc and each of these parameters can be blank. If one of them is blank and the rest of them have some valid values, then I..

Can you SELECT TOP @HOWMANY in a stored proc? - I'd like to create a stored proc where the number of records returned is dynamic, based upon an input parameter, such as: Create MyProc (@HowMany int) as Select TOP @HowMany * from MyTable Of course, I get a syntax error with the above. But is there ...

Can I use the results of a stored proc in a SELECT stateme.. - This is probably a classic question: what is the TSQL syntax to use the results of a SP in a statement? something like: select * from sp_spaceused (which doesnt work) or any other proc? thanks Jonathan

Can a stored proc call another stored proc? - and if so what happens if I am within a cursor, can I come back to it and continue? Thanks.

stored proc help - hey all, i'm trying to retrieve to fields from a database so i can use thru out my procedure. if i use SELECT INTO a local table do i have to define the fields for that local table? is there another way? thanks, rodchar
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]