Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Thursday, March 22, 2012

Combine two stored procedure results

Hi,
I have two databases DB2006, DB2005.I have the Stored Procedure getdata which has the 2 parameters startdate and end date.This Stored procedure exist in all databases.

STored procedure CallGetdata
@.startdate datetime
@.enddate datetime
If startdate < 1/1/2007 the call getdata in the DB2006
if startdate <1/1/2006 then call getdata in the DB2005.
Here the problem is if startdate is 6/1/2005 and Enddate is '3/1/2006' then combine the stored procedure results from the DB2006 and DB2005 databases.
I have one idea i.e create a temp table and insert the two Stored procedure results into it.
Create #table1(name varchar(20))
insert into #table1 exec DB2006.dbo.getdata
insert into #table1 exexc DB2005.dbo.getdata
Select * from #table1
drop table #table1.
Anyone please give me better idea than creating temp table.

Thanks in advance

Can you change getdata (or is this something your stuck with?)

Another possible option is to turn your getdata stored procedure into a table valued function. This means you won't be able to exec it directly though, you will have to access it in a query. Then you could do:

select * from db2006.dbo.getdata(...) union select * from db2005.dbo.getdata(...).

This would most likely perform better (although I can't say for sure). If getdata() is one query (or can be turned into one query), then you can use an inline table valued function, and this will definitely perform better then the temp table solution.

Also, your probably better off using a table variable in this case as opposed to a temp table.

|||

if you are stuck with the stored procedure as-is, then your idea is the best one. Unless the proc is extremely complex (and with a name like getdata, it is not going to be easy for us to guess :) then building a database for queries that span databases is a better idea and union the results together. Or consider the ideas that Adam has given also.

I would consider not having databases with the year in the name, and just have one database that spans years, personally. That is clearly the most solid answer and will make your reporting easier. If this wasis a performance idea, there are ways to make this work far better than with multiple databases. And if both databases are on the same drive, you are possibly not saving much...

|||

Thank you very much for your ideas.For the reports the stored procedures already created.But now to imrove the performance they created the separate 3 databases one for current year and other for previous year and remaining(all previous years are in Hist databases).Now I need to migrate the existing stored procedure to all databases all working fine but the problem is when they enter startdate which is in one year and end date in another year, in this case we need to combine the results of two stored procedures from two databases.Thatswhy I created a separate stored procedure and temp table is used for combining the two SP results.

Thanks

|||

Hi,

Which one give better performance whether the Stored procedure with table datatype to insert the combined results from two databases, Or table valued functions.

Thanks.

Saturday, February 25, 2012

column reference as parameter of sp

Is it possible to have a column reference as the parameter of a stored
procedure? The books say parameters have to be literal expressions, so
I'm guessing it's not possible, but I thought I'd ask anyway.
I want to do something like this:
INSERT INTO Table1 ([Column1]) (SELECT ? FROM Table2)
The single, unnamed input parameter needs to be the name of one of the
columns in Table2, such as [Column2]. I want it to be a stored
procedure because the query must run many times with alternating column
references and literals.
ShaileshEither
exec a string, or look at using sp_executesql... either will work... both
are doc'd in books on line.
"Shailesh Humbad" <humbads1@.hotmail.com> wrote in message
news:FrmYa.246417$BA.59826426@.twister.columbus.rr.com...
> Is it possible to have a column reference as the parameter of a stored
> procedure? The books say parameters have to be literal expressions, so
> I'm guessing it's not possible, but I thought I'd ask anyway.
> I want to do something like this:
> INSERT INTO Table1 ([Column1]) (SELECT ? FROM Table2)
> The single, unnamed input parameter needs to be the name of one of the
> columns in Table2, such as [Column2]. I want it to be a stored
> procedure because the query must run many times with alternating column
> references and literals.
> Shailesh
>

Column or Field variable for an Excel query.

Hello all.
I have an excel query that pulls data from a SQL database. I have 2 parameters that represent the [start] and [end] of a between criteria. Here is the query.

SELECT CltDue.CDClientName
, CltDue.CDEventDesc
, CltDue.CDDescription
, (empfname)+' '+(emplname)
, CltDue.CDTargetAmount
, CltDue.CDTargetHours
FROM VPM.dbo.CltDue CltDue, VPM.dbo.Employee Employee
WHERE CltDue.CDInCharge = Employee.ID
AND ((CltDue.CDStart Between ? And ?))

This works fine, but what i wish to do is create a 3rd parameter for CltDue.CDStart
There are 4 fields in the CltDue table that i need.CDstart,CDdateComplete, CDTarget, and CDDateDelivered.
I want the user to be able to choose one of these 4 from a list box and then enter the beginning and end date.
Is excel capable of this?
Thanks for any help.

Quote:

Originally Posted by silversubey

Hello all.
I have an excel query that pulls data from a SQL database. I have 2 parameters that represent the [start] and [end] of a between criteria. Here is the query.

SELECT CltDue.CDClientName
, CltDue.CDEventDesc
, CltDue.CDDescription
, (empfname)+' '+(emplname)
, CltDue.CDTargetAmount
, CltDue.CDTargetHours
FROM VPM.dbo.CltDue CltDue, VPM.dbo.Employee Employee
WHERE CltDue.CDInCharge = Employee.ID
AND ((CltDue.CDStart Between ? And ?))

This works fine, but what i wish to do is create a 3rd parameter for CltDue.CDStart
There are 4 fields in the CltDue table that i need.CDstart,CDdateComplete, CDTarget, and CDDateDelivered.
I want the user to be able to choose one of these 4 from a list box and then enter the beginning and end date.
Is excel capable of this?
Thanks for any help.




As with any of the VBA applications excel included you should be able to create either an dynamic SQL statement including your column values from a selectable list and then fire the SQL to the SQL server to retrieve your dataset or even better reference an stored procedure either way....you are in the SQL Server forum so maybe you are better served in an Excel forum to follow through on the VBA application side of it?

Regards

Jim :)|||Thanks, I'll give it a try

Column Names with a cursor

I have set up a query where I am using a cursor to pass result from one
query to use as select parameters in another query. The problem I am
having is that when I pass the results into the second query I get the
column headers?

How do you suppress the column headers from showing in the query? I
see how you select options - print headers.

Thanks

JasonIf you dont want to have headers, then you can use ' '

Select col1 as ' ', col2 as ' ' ...... from table

Madhivanan|||Why use a cursor at all? You should be able to make the first query into a
correlated subquery.

--
David Portas
SQL Server MVP
--

Sunday, February 12, 2012

Collection was modified after the enumerator was instantiated

When I set the parameters on a ReportViewer and click the ViewReport
button, the "Collection was modified after the enumerator was
instantiated" error is thrown.
Acessing the report directly through IE works perfectly. The problem
only occurs in the ReportViewer.
Is this a bug? Is there any work-around?
Scenario:
- SQL 2005 April CTP running on Windows Server 2003 SP1
- Visual Studio 2005 Beta 2
- ReportViewer Control for WebForms, acessing a server report
Thanks in advance,
AndersonHavign same problem: using ASPNET as role and now can access report, then
when setting parameters in toolbar and running report, get the same error
below. Anyone have some insight. I'm using SQL RS 2005 and SQL 2005 Server
"Anderson dos Santos Pimentel" wrote:
> When I set the parameters on a ReportViewer and click the ViewReport
> button, the "Collection was modified after the enumerator was
> instantiated" error is thrown.
> Acessing the report directly through IE works perfectly. The problem
> only occurs in the ReportViewer.
> Is this a bug? Is there any work-around?
> Scenario:
> - SQL 2005 April CTP running on Windows Server 2003 SP1
> - Visual Studio 2005 Beta 2
> - ReportViewer Control for WebForms, acessing a server report
>
> Thanks in advance,
>
> Anderson
>