Showing posts with label procs. Show all posts
Showing posts with label procs. Show all posts

Wednesday, March 7, 2012

Help understanding stored procs

I am having trouble understanding how to correctly use stored procs. Here is what I think the process is. Correct me where I am wrong.

I am developing a read-only program with VB 2005 as a front end and SQL Server back end. The user will enter an ID# for search criteria which will be passed as a parameter to a stored proc. I want to create a stored proc that alters a view by changing the ID# the view filters on. A dataset will be created in the front end from that view.

So in SSMS, I create a new proc like this:

CREATE PROC {blah, blah}

@.IDnum

AS

BEGIN

ALTER VIEW {blah, blah}

AS

SELECT {blah, blah}

FROM {blah}

WHERE blah.ID = @.IDnum

END

I would run the code to create the proc, then modify it to an ALTER PROC. I've tried this without success. What am I missing. Yes, I am new to this.

Thanks for the help

Hi,

why do you want to alter a view, rather than using a filter on the view (although the view isn′t that column rich :-) )

So you could create the View as

CREATE VIEW SomeView
AS

SELECT col1, col2 From SomeTable

and then select using

SELECT * FROM SomeView Where Col1 = 'SomeId'

HTH, Jens Suessmeyer

http://www.sqlserver2005.de

|||

Thanks for the reply. I am entering the world of SQL Server from an Access background. In Access I often change the filtering of a query by changing the sql with code or by referencing a field on a form from the criteria field of a query.

That is the thinking with which I have approached views. The purpose of my post is to check my thinking against the experienced community and hopefully get better ideas and understanding. Your reply helps. Thanks again.

|||You′re welcome :-)

Friday, February 24, 2012

Help retrieving values from 2 similar stored procs

I created one stored proc, then copied it to create another similar stored proc with just some filtering changes in the second. Now I want to obtain the results from both like this:
ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Group Name PostedAmount
---------
Row# 1 from current stored proc
Row # 2 from called stored proc
so something like this I should get in the end for example when both results are combined:
ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Group Name PostedAmount
---------
100000 33455 200 300 345555 4455555 5666666 Arizona 56000
103400 22455 900 700 777555 3333555 5444666 Illinois 660000

The first stored proc (CurrentMonthCollections_AZ) attempts to include the results of the second stored proc at the end (CurrentMonthCollections_IL) by calling it and inserting the fields into a temp table.
I was told by Angel to instead convert the second stored proc into a UDF...but having trouble with this.
What is the best approach to get the desired 2 rows back that I need in that fasion?
Here is the code for the 2 stored procs (very long so I will post as as links):
http://www.webfound.net/CurrentMonthCollections_AZ.txt
http://www.webfound.net/CurrentMonthCollections_IL.txt
Look at the end of CurrentMonthCollections_AZ.txt to see where I'm stuck in trying to select the results (ProjFee ProjGross DailyRunRate Var1 InHouse1 InHouse2 GrossGoal Var1 PostedAmount
) from both stored procs.
I don't think UNION is what I want because it will combine stuff...I just want 2 separated rows

"I don't think UNION is what I want because it will combine stuff...I just want 2 separated rows"

Union is what you want:

select 1
union
select 2

returns


1
2

It seems to me that you could parameterize the procedures and get back the two rows that you want, or at least have one procedure and pass in the state you are interested in...that is a lot of proc to deal with though...

|||what about union all ?|||

The difference between the 2 is that union removes duplicates and union all does not

union all is also faster because of that

select 1
union
select 2
union
select 2


select 1
union all
select 2
union all
select 2

Denis the SQL Menace

http://sqlservercode.blogspot.com/

|||

Sorry, I should have been clearer:

UNION ALL doesn't eliminate duplicates, UNION does.

UNION ALL is probably the more correct way to go in this case, but it won't really make a difference with such a small set.

|||in this case, there are no dups so it doesn't matter I guess then.|||so I thought UNION puts it all in one row? I must be wrong...