Showing posts with label calls. Show all posts
Showing posts with label calls. Show all posts

Tuesday, March 27, 2012

Calling Stored Procedure from a stored procedure on where clause (URGENT!)

I need a stored procedure that include dynamic where clause. so I
created these two sp. GetCampaigns calls Checkvalues sp to complete
where clauseb but it doesn't work. any idea ?
CREATE PROCEDURE GetCampaigns (@.CampaignTypeId int,@.CardTypeId int)
AS
SELECT Campaigns.* FROM Campaigns
WHERE (CheckValues 'Campaigns.CampaignTypesId',@.CampaignTypeId) AND
(CheckValues 'Campaigns.CardTypesId',@.CardTypeId)
GO
CREATE PROCEDURE CheckValues(@.InitialStr nvarchar(30),@.Type int)
AS
IF((@.Type = 1) OR (@.Type = 0))
SELECT @.InitialStr + '!=0'
ELSE
SELECT @.InitialStr + '=' + @.Type
GOUse a function and not a procedure for CheckValues
Daniel EYER
"ONEY" <ozcankanbur@.gmail.com> a crit dans le message de
news:c16976dd.0502220317.735fa507@.posting.google.com...
> I need a stored procedure that include dynamic where clause. so I
> created these two sp. GetCampaigns calls Checkvalues sp to complete
> where clauseb but it doesn't work. any idea ?
>
> CREATE PROCEDURE GetCampaigns (@.CampaignTypeId int,@.CardTypeId int)
> AS
> SELECT Campaigns.* FROM Campaigns
> WHERE (CheckValues 'Campaigns.CampaignTypesId',@.CampaignTypeId) AND
> (CheckValues 'Campaigns.CardTypesId',@.CardTypeId)
> GO
> CREATE PROCEDURE CheckValues(@.InitialStr nvarchar(30),@.Type int)
> AS
> IF((@.Type = 1) OR (@.Type = 0))
> SELECT @.InitialStr + '!=0'
> ELSE
> SELECT @.InitialStr + '=' + @.Type
> GO|||Oney
Don't use dynamic WHERE condition ,instead read this great article
http://www.sommarskog.se/dyn-search.html
"Daniel Eyer" <daniel.eyer@.free.fr> wrote in message
news:421b2391$0$1226$8fcfb975@.news.wanadoo.fr...
> Use a function and not a procedure for CheckValues
> Daniel EYER
> "ONEY" <ozcankanbur@.gmail.com> a crit dans le message de
> news:c16976dd.0502220317.735fa507@.posting.google.com...
>sql

Thursday, March 22, 2012

Calling nested stored proedure many times

There are two stored procedures. One calls the other one time. It works.
create procedure nestedSP
@.p float
as
declare @.q float
set @.q = @.p
select @.q
go
create procedure CallingNestedSP
@.p float
as
exec nestedSP @.p
go
exec CallingNestedSP 111.11
How do I get the resultset as 111.11, 111.11 by calling the nexted twice.
Please advise. Thanks
PeterHi Peter
There is no best way of joining the two results from SPs ...
You can maybe try to create the called procedure as a UDF and
call it twice.Or if you only have to use a SP,
then insert the result into a temp table. Your calling SP will have the code
like this..
create procedure CallingNestedSP
@.p float
as
begin
create table #temp (a decimal(...))
INsert into #temp
exec nestedSP @.p
select a,a from #temp
end..
Though I would prefer a udf for a situation similar to yours.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/

Calling my web service from the custom code is failing

This is what I'm trying to achieve. I want to call into my custom
assembly from the reports, and that assembly in turn calls a web
service. The reason I have to do that is because I think we can't
directly call a web service from the reports. To complicate the things
further I also have Forms Authentication enabled on Reporting
services.
Forms authentication enables me to use User!UserID which I pass on to
my custom assembly. The assembly in turn passes the parameter to web
service. To test the functionality I was trying to return simply
"Hello " + parameter value from the web service.
I have copied the custom assembly code to report manager's bin and
report server's bin directories. I have also added CodeGroup to
rssvrpolicy.config file to give a "FullTrust" permission on my custom
assembly.
The reports works as expected in Preview tab in VS.NET designer, but
when accessed from report manager or from the report server via URL,
the expression in text box shows #Error.
Please help to explain how to debug and fix the problem. Thanks much.
YogeshI have finally managed to get web service call working. I was able to
do this by asserting System.Net.WebPermission. Here's code snippet:
//Add following at the top of the class file
using System.Net;
using System.Security.Permissions;
//Add these lines just before instantiating web service proxy class
WebPermission webPerm = new
WebPermission(PermissionState.Unrestricted);
webPerm.Assert();
Remember to add exception handling code otherwise when functions in
custom code fails you will get "#Error" in the textbox in the report
without explaining what went wrong. If your methods return strings
then for debugging purposes you can return ex.ToString() which will at
least let you know what went wrong.
Yogesh|||Hi Yogesh,
Help..
Tried all of the above and still getting the #error on my report - I can
call a different function in the dll (not using a web service) and that is
fine..
Do I need to set up any policies in the rssvrpolicy.config file ?
I have added the reference in my code Web.Assert code - so it looks like I
still have problems with the config files ..
"Yogesh" wrote:
> This is what I'm trying to achieve. I want to call into my custom
> assembly from the reports, and that assembly in turn calls a web
> service. The reason I have to do that is because I think we can't
> directly call a web service from the reports. To complicate the things
> further I also have Forms Authentication enabled on Reporting
> services.
> Forms authentication enables me to use User!UserID which I pass on to
> my custom assembly. The assembly in turn passes the parameter to web
> service. To test the functionality I was trying to return simply
> "Hello " + parameter value from the web service.
> I have copied the custom assembly code to report manager's bin and
> report server's bin directories. I have also added CodeGroup to
> rssvrpolicy.config file to give a "FullTrust" permission on my custom
> assembly.
> The reports works as expected in Preview tab in VS.NET designer, but
> when accessed from report manager or from the report server via URL,
> the expression in text box shows #Error.
> Please help to explain how to debug and fix the problem. Thanks much.
> Yogesh
>

Tuesday, March 20, 2012

Calling another record into this record

This has got to be easier than I am making it out to be. I have a
query that calls on two tables linked by unique ids. I would like to
have the previous outing's # of laps brought into the current outing.
An example of what the view follows
SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
FROM dbo.vwOutings [Main Outing] INNER JOIN
dbo.vwLaps ON [Main Outing].OutingID =
dbo.vwLaps.OutingID
GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
ORDER BY [Main Outing].OutingNumber DESC

returning
Outing Number Outing ID
# of laps
6{87C29BDC-A2D1-4606-A87B-D456F702BFCE}99
5{203319A9-4B2F-406A-9454-0DD3D5F86211}37
4{B6306DF5-5BF0-4456-8E06-0CD2ED8B1874}54
3{A70643F3-28C2-47CC-B623-4BD6904C2825}95
2{EB409D12-C79C-4D96-BE2A-0BD49C5BE69D}80
1{75D7793D-3239-4544-BACC-84EB564C8638}36
what I woud like is
Outing Number Outing ID
# of laps # of laps previousouting
6{87C29BDC-A2D1-4606-A87B-D456F702BFCE}99 37
5{203319A9-4B2F-406A-9454-0DD3D5F86211}37 54
4{B6306DF5-5BF0-4456-8E06-0CD2ED8B1874}54 95
3{A70643F3-28C2-47CC-B623-4BD6904C2825}95 80
2{EB409D12-C79C-4D96-BE2A-0BD49C5BE69D}80 36
1{75D7793D-3239-4544-BACC-84EB564C8638}36 0
sorry if this is a silly question I'm a little new to this thanks for
any helpOn 15 Jun 2006 05:47:43 -0700, pltaylor3@.gmail.com wrote:

>This has got to be easier than I am making it out to be. I have a
>query that calls on two tables linked by unique ids. I would like to
>have the previous outing's # of laps brought into the current outing.
>An example of what the view follows
>SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
>Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
>FROM dbo.vwOutings [Main Outing] INNER JOIN
> dbo.vwLaps ON [Main Outing].OutingID =
>dbo.vwLaps.OutingID
>GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
>ORDER BY [Main Outing].OutingNumber DESC
(snip)

Hi pltaylor3,

First, why do you have a "TOP 100 PERCENT" in your query? Since 100
percent equals all rows, you're actually requestin alll rows - which is
the default if you don't specify a TOP clause at all! Get rid of it.

Next, why do you choose aliases that require you to escape the names?
Are you really that fond of typing lots of [ and ] chartacters?

Third, you might also reconsider the "vw" prefixes on your views. In SQL
Server, objects should be named for what they represent, not for how
they happen to be implemented.

Last, here's an answer to your question:

SELECT mo.OutingNumber, mo.OutingID,
COUNT(l.LapTime) AS "# of laps",
(SELECT COUNT(l2.LapTime)
FROM dbo.Outings AS mo2
INNER JOIN dbo.Laps AS l2
ON mo2.OutingID = l2.OutingID
WHERE mo2.OutingNumber = mo.OutingNumber - 1) AS "# of
laps previousouting"
FROM dbo.Outings AS mo -- [Main Outing]
INNER JOIN dbo.Laps AS l
ON mo.OutingID = l.OutingID
GROUP BY mo.OutingNumber, mo.OutingID
ORDER BY mo.OutingNumber DESC

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP|||(pltaylor3@.gmail.com) writes:
> This has got to be easier than I am making it out to be. I have a
> query that calls on two tables linked by unique ids. I would like to
> have the previous outing's # of laps brought into the current outing.
> An example of what the view follows
> SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
> Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
> FROM dbo.vwOutings [Main Outing] INNER JOIN
> dbo.vwLaps ON [Main Outing].OutingID =
> dbo.vwLaps.OutingID
> GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
> ORDER BY [Main Outing].OutingNumber DESC

First remove the TOP 100 PERCENT and ORDER BY from the view. They mean
nothing. In SQL 2000 it may seem that a SELECT from the view without
an ORDER BY always return rows according to the ORDER BY in the view,
but that is mere chance. In SQL 2005, this does not happen.

The only way to get sorted output from a query in SQL (no matter the
engine) is to have an ORDER BY clause in the SELECT statment that
outputs the data to the client.

> returning
> Outing Number Outing ID
> # of laps
> 6 {87C29BDC-A2D1-4606-A87B-D456F702BFCE} 99
> 5 {203319A9-4B2F-406A-9454-0DD3D5F86211} 37
> 4 {B6306DF5-5BF0-4456-8E06-0CD2ED8B1874} 54
> 3 {A70643F3-28C2-47CC-B623-4BD6904C2825} 95
> 2 {EB409D12-C79C-4D96-BE2A-0BD49C5BE69D} 80
> 1 {75D7793D-3239-4544-BACC-84EB564C8638} 36
> what I woud like is
> Outing Number Outing ID
> # of laps # of laps previousouting
> 6 {87C29BDC-A2D1-4606-A87B-D456F702BFCE} 99 37
> 5 {203319A9-4B2F-406A-9454-0DD3D5F86211} 37 54
> 4 {B6306DF5-5BF0-4456-8E06-0CD2ED8B1874} 54 95
> 3 {A70643F3-28C2-47CC-B623-4BD6904C2825} 95 80
> 2 {EB409D12-C79C-4D96-BE2A-0BD49C5BE69D} 80 36
> 1 {75D7793D-3239-4544-BACC-84EB564C8638} 36 0
> sorry if this is a silly question I'm a little new to this thanks for
> any help

SELECT MO.OutingNumber, MO.OutingID,
COUNT(L.LapTime) AS [# of laps],
COUNT(L2.LapTime) AS [# of prev laps],
FROM dbo.vwOutings MO
JOIN dbo.vwLaps L ON MO.OutingID = dbo.vwLaps.OutingID
LEFT JOIN (dbo.vwOutings MO2
JOIN dbo.vwLaps L2 ON MO2.OutingID = L2.OutingID)
ON MO.OutingNumber = MO2.OutingNumber + 1
GROUP BY MO.OutingNumber, MO.OutingID

Bur this is a complete guess about what your tables and data are like. If
it does not work out, please post:

o CREATE TABLE statements for your tables.
o INSERT statements with the sample data.

Some narrative how the tables are organised would help too.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Monday, March 19, 2012

Calling a Stored Procedure that does an insert and returns one value

Hi !

I have trying to do an insert with a subroutine that calls a stored procedure, but it doesn' t run. The page loads properly but nothing is inserted in the table of the database, no errors appears after submit the form.

<asp:Button id="SubmitButton" OnClick="Send_data" Text="Submit" runat="server"/>

Here is the code:


Sub Send_data(Sender As Object, E As EventArgs)

Dim CmdInsert As New SqlCommand("new_user1", strConnection)
CmdInsert.CommandType = CommandType.StoredProcedure

Dim InsertForm As New SqlDataAdapter()
InsertForm.InsertCommand = CmdInsert

CmdInsert.Parameters.Add(New SqlParameter("@.RETURN_VALUE", SqlDbType.bigint, 8, "User_id"))
CmdInsert.Parameters("@.RETURN_VALUE").Direction = ParameterDirection.ReturnValue

CmdInsert.Parameters.Add(New SqlParameter("@.e_mail", SqlDbType.varchar, 50, "e_mail"))
CmdInsert.Parameters("@.e_mail").Value = mail.Text()

End Sub

What it lacks?cmdInsert.ExecuteNonQuery|||Now when I submit the form the error is:

System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed.

But the connection is available because in my webform appears several 'dropdownlists' with data from the database... And the 'strConnection' is the correct 'SqlConnection' declared before this subroutine like this: Dim strConnection As New SqlConnection(ConfigurationSettings.AppSettings("myConn"))|||If I put this runs:


...
strConnection.open()
cmdInsertar.ExecuteNonQuery
strConnection.close()

End Sub

Is this a good way of doing the insert with the called SP?
Why is not enough with this?:


Dim CmdInsert As New SqlCommand("new_user1", strConnection)

Sunday, March 11, 2012

Calling a stored procedure and pass parameter one by one

Hi,

I need to create a batch process which calls a stored procedure.

Here's the scenario.

I have 3 tables

Theater - TheaterId, TheaterName, Revenues,locationid, stateid
State - StateId, StateName
Location - LocationId, LocationName, StateId

There is a stored procedure spoc_updateTheater that accepts the state and location id and runs a set of sql statements against the theater table. However i want this to run for all the locations in a state one by one. There are some 700 locations in 45 states. How do i pass the location and state id one by one to the stored proc. Can i call this from a commandline or run it as batch process?

vidkshi

You will have iterate through the filtered data of the first procedure and execute the procedure one by one, the is no command to do this you will have to do that manually. Would look like the following pseudo code.

TableContent = YouSelectquery

Loop throught the data and pick one row per iteration
{

Execute your procedure with the values from the picked rows.

}

You can either do this using a cursor (preferable fast forward / read ony if you do not want to change anything) or use a tamp table.

Jens K. Suessmeyer

http://www.sqlserver2005.de

|||

Hi,

I will appreciate if someone could tell me the sql for it :

Here's the Theater table and the data. I want to loop through every state and location in the state/location tables and pass that stateid locationid to the stored proc.

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Theater]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Theater](
[TheaterId] [int] NULL,
[TheaterName] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Revenues] [bigint] NULL,
[StateId] [int] NULL,
[LocationId] [int] NULL
)
END
GO
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (1, N'T1 ', 10000, 1, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (2, N'T2 ', 2333, 1, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (3, N'T3 ', 234234, 1, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (4, N'T4 ', 43454, 1, 2)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (5, N'T5 ', 4454, 1, 2)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (6, N'T6 ', 11132, 2, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (7, N'T7 ', 345, 2, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (8, N'T8 ', 675445, 2, 2)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (9, N'T9 ', 344556, 3, 1)
INSERT [dbo].[Theater] ([TheaterId], [TheaterName], [Revenues], [StateId], [LocationId]) VALUES (10, N'T10 ', 23234, 3, 1)

vidkshi|||

What about:

Code Snippet

CREATE PROCEDURE spUpdateTheater

/*

Written by Jens K. Suessmeyer (c) http://www.sqlserver2005.de

*/

AS

BEGIN

SET NOCOUNT ON

DECLARE @.TheaterData Table

(

COUNTER INT IDENTITY(1,1),

Theaterid INT NULL,

StateId INT NULL

)

DECLARE @.ROWCOUNT INT

DECLARE @.I INT

SET @.I = 1

DECLARE @.Theaterid INT

DECLARE @.StateId INT

INSERT INTO @.TheaterData

(

Theaterid,

StateId

)

SELECT DISTINCT

TheaterId,

Stateid

FROM Theather

SET @.ROWCOUNT = @.@.ROWCOUNT

WHILE @.I <= @.ROWCOUNT

BEGIN

SELECT

@.Theaterid = TheaterId,

@.StateId = Stateid

FROM @.TheaterData

WHERE COUNTER = @.ROWCOUNT

EXEC spExecuteYourUpdateProcedure @.Theaterid,@.StateId

SET @.I = @.I +1

END

END

Jens K. Suessmeyer

http://www.sqlserver2005.de

|||

Hi,

Thanks for the code. I was wondering why you added the theater id and stateid in the temp table whereas it had to be stateid and locationid. Anyways i changd your query to look like this. I also tried running your stored proc as is but with no results.

Code Snippet

ALTER PROCEDURE [dbo].[spUpdateTheater]
/*

Written by Jens K. Suessmeyer (c) http://www.sqlserver2005.de

*/

AS

BEGIN

SET NOCOUNT ON

DECLARE @.TheaterData Table

(

COUNTER INT IDENTITY(1,1),

Locationid INT NULL,

StateId INT NULL

)

DECLARE @.ROWCOUNT INT

DECLARE @.I INT

SET @.I = 1

DECLARE @.Locationid INT

DECLARE @.StateId INT

INSERT INTO @.TheaterData

(

Locationid,

StateId

)

SELECT DISTINCT

LocationId,

Stateid

FROM Theater

select * from @.TheaterData

SET @.ROWCOUNT = @.@.ROWCOUNT

WHILE @.I <= @.ROWCOUNT

BEGIN

SELECT

@.Locationid = LocationId,

@.StateId = Stateid

FROM @.TheaterData

WHERE COUNTER = @.ROWCOUNT

EXEC spoc_Theater @.StateId, @.Locationid

SET @.I = @.I +1

END

END

My store proc spoc_updatetheater looks like this

Code Snippet

ALTER proc [dbo].[spoc_Theater]
@.stateid int,
@.locid int
as
update theater
set revenues = 20000
where stateid = @.stateid and locationid = @.locid
and revenues > 10000

I tried running it however I see there is no change in the Theater table after the query executes.

Any reasons?

Vids

|||

Yes, you are right and in addition you should change the

Code Snippet

WHERE COUNTER = @.ROWCOUNT

to

Code Snippet

WHERE COUNTER = @.I

Jens K. Suessmeyer

http://www.sqlserver2005.de
|||

Cool. thanks.

you also mentioned that the same query could be written using a cursor. What advantage will i get.

In this query we are creating a temporary table, filling data and looping through it. I expect around 5000 different combinations of state and location id in this temporary table. Will it be ok from a performance point to use the temporary table?

Cant i just use the location table as it will contain a distinct location and state combination. So instead of the temp table, i just loop through the location table and execute the proc for each combination of location and state. How will we do this using a cursor and how will it improve performance?

Vidkshi.

|||

I guess you won′t recognize the difference with 5000 rows. You can sure switch over to the locations tables.

Jens K. Suessmeyer

http://www.sqlserver2005.de

|||I don't have an answer to your question but you might want to consider removing the stateid from your Theater table as it violates 3rd normal form.

Theater - TheaterId, TheaterName, Revenues,locationid, stateid
State - StateId, StateName
Location - LocationId, LocationName, StateId

Calling a DTS Stored Procedure from ASP.NET

I have a stored procedure that calls a DTS package to grab a text file that has been uploaded to the server and merge it with a table on the database. The DTS package works woderfully in SQL, as does the the file upload. The problem arrises when I create a stored procedure to run the DTS package. I know that you have to shell out and do a command line on the SQL server (and I think that I got the syntax correct) but its calling the Stored Procedure in the ASP.NET app that is causing me hardship. Here is the code that I have so far:

Stored Procedure:

CREATE PROCEDURE spSampleData AS exec master..xp_cmdshell 'dtsrun /SZEUS\sqlServer113 /NdtsPackage /UuserID /Ppassword'
GO

VB to run DTS:

Dim myCommand As SqlCommand
myCommand.CommandType = CommandType.StoredProcedure
myCommand.CommandText = "spSampleData"
myCommand.ExecuteNonQuery()

I'm not sure what I am doing wrong but any help would be great.

Thanks!Your code seems ok on the face of it. What is the error message that is generated? Or does just nothing happen?

These links might be helpful:

DTS Cookbook for .Net

Execute a package from T-SQL This one describes your current method but offers another alternative too.|||Thank you so much for the links - I'll check them out. So here are the errors...

When running in Query Analyzer:

-"DTSRun: Cannot create COM Server to load and execute DTS Package. Error -2147024769 (8007007F): The specified procedure could not be found. "

I thought this was because some of the DLLs weren't registered but that isn't it.

-"I tried to schedule it as a job, before I got this just to see, and it gave me a a long hex dec error that was in a windows alert box, so I didn't even worry with that. But it was:"

The procedure entry point ?SQLUIUpdateRegistryFromResource could not be located in the dynamic link library SQLRESLD.dll

In the Webform, it was your typical:

"Object reference not set to an instance of an object" error... So I'm at a loss.|||I hate those sorts of problems. I Googled the error message and ended up with this link.