Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Tuesday, March 27, 2012

Executing N procedures in 1 Round trip

w/ SqlServer, is there anyway to pack a number of calls to the same stored procedure into a single round-trip to the DB short of dynamically writing a T-SQL block? For example, if I'm calling a procedure "Update Contact" which takes 2 params @.Campaign, @.Contact 20 times how would I pass in the values for those 20 diffrent versions?

You could pass all the parameters to another stored procedure as a delimited list and parse them over there, create a loop, and call the stored proc in the loop.

|||I'm sure I could. I just thought I'd heard of something similar to ODP.Net's ArrayBinding syntax for SqlServer. I'm fairly sure I wasnt thinking about bulkcopy.|||

To my knowledge I dont think there is any standardized way to make multiple calls in one trip. Perhaps someone else here knows if there's any new feature in 2005. I havent been doing much development in 2005.

|||hrm...

Will the SQL Server provider let me execute blocks of t-sql?

eg something like:

AddContact(@.C1, @.U1);
AddContact(@.C2, @.U2);
AddContact(@.C3, @.U3);
...
AddContact(@.CN, @.UN);
go;|||

Yes, however, you must specify that the call type is text, not stored procedure, and you must properly format your calls like:

EXECUTE AddContact @.C1,@.U1
EXECUTE AddContact @.C2,@.U2
...
GO

|||So what is the "proper" format for sp calls in a 'anonymous block'.

Monday, March 26, 2012

Executing a View timesout through Open View

Hello All,

I have created a view that will potentially return a huge number of records around 500,000 rows. When i execute this view by clicking Open View from the SQL Management studio i get the following error

SQL Execution error

Error Source: .Net SqlClient Data Provider
Error Message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

However the query executes when paste the generated query of the view in the query designer window. It returns the 500,000 rows. I am wondering if there is some option that i will have to configure to return a large size result set in view designer. I also want to know if this is an inherent problem with view designer.

Any help regarding this will be really appreciated.

Satya

Open View creates a fully editable grid of the rows returned by the view so it is not surprising that it is choking on that much data.

First - this is not the view designer, the view designer opens when you select Modify, and that will let you edit the view definition.

If you do want to work with the data in the view through Open View, then after you select Open View, click the stop button down at the bottom of the window, next to the message that says Retrieving Data... It will stop and show whatever data it retrieved before you clicked the button. Now click the Show SQL Pane button on the toolbar (or Query Designer/Pane menu). In the SQL pane, edit the query by adding a WHERE clause to the SELECT statement that is selecting the data from you view. Now click the Execute SQL button (the exclamation mark) and it will run and return only the rows that match. Now you can selectively pull only a portion of the 500,000 rows at a time and work on them, then change the WHERE clause to select other rows. I can't believe you really want all 500,000 rows on screen at the time - and SQL Server doesn't think it's a good idea either :-)

|||

Hi,

Thanks for your reply. I didnt intend to see all the 500,000 rows of data at a time but did want to know the maximum volume that i can see when i click Open View. The stop button is also disabled when i click Open view and i get this error directly and so i cannot see a partial set of the data the way you had specified. Also i wrote <select a top 1 * > query enveloping the query generated by the view so that i could get atleast the first tuple of the query result but even this was aborted by the same error. The error comes immediately after a minute and no option is enabled for me to do anything once i click Open View or execute.

I was trying to see if this was a configuration issue in VIEW or if i was missing something else. The same query does give the intended results when i run in the query designer. Is there a capacity constraint for the Results grid pane in the View Designer as compared to normal query window.

Thanks again for your help and would appreciate if you can clarify the above too

satya

|||Do you by any chance have a TOP and an ORDER BY in the view definition, or is the view just a standard SELECT?|||It just seems that the CommandTimeout property is not updated in Management Studio when using View, and that it is always stuck at 30 sec. I have the same problem, but it seems impossible to fix.|||I have a view that returns over 500,000 rows, it takes about 5 minutes and never times out. I can click the Stop button at any time. I haven't changed anything from default so I'm not sure what's different.sql

Monday, March 19, 2012

ExecuteNonQuery to count number of rows?

My understanding from a previous thread was that ExecuteNonQuery() could be used to display the number of rows returned.

Does this also work when calling stored procedures and passing parameters?

I have code (shown) that perfectly calls and returns Distinct models downloaded by Country. Yet the rowCount variable displays a -1.

What should I do?

Dim myCommandAs New SqlClient.SqlCommandmyCommand.CommandText ="ap_Select_ModelRequests_RequestDateTime"myCommand.CommandType = CommandType.StoredProceduremyCommand.Parameters.AddWithValue("@.selectDate", dateEntered)myCommand.Parameters.AddWithValue("@.selectCountry",CInt(selectCountry))myCommand.Connection = concon.Open()Dim rowCountAs Integer = myCommand.ExecuteNonQuery()numberParts.Text = rowCount.ToStringcon.Close()
Thank you.

Check yourap_Select_ModelRequests_RequestDateTimestored procedure, and if you find:

SET NOCOUNT OFF

useually at the end of SP, remove it and I hope this will work.

Good luck.

|||

Hi SolitaryMan,

Normally one would use ExecuteReader for SELECT statements, which you seem to be doing, and call sqlDataReader.RecordsAffected to get the number of rows affected.

Getting -1 from ExecuteNonQuery for a SELECT statement is documented behavior:

"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1." --http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(VS.71).aspx

As for passing parameters to a stored proceudre with ExecuteNonQuery, that should not affect the return value. ExecuteNonQuery does indeed return the number of rows affected, just not for SELECT statements for example.

Here's some sample code for using SqlDataReader to get the number of rows affected from a SELECT Statement:

SqlCommand selectStatement =newSqlCommand();

selectStatement.CommandText ="PROC_SELECT_ITEM";

selectStatement.CommandType =CommandType.StoredProcedure;

selectStatement.Connection = connection;

SqlDataReader reader = selectStatement.ExecuteReader();

int rowsAffected = reader.RecordsAffected;

Pete

|||

You can use @.@.ROWCOUNT in your stored procedure and assign it to an output parameter.

|||

Peter Lee:

Hi SolitaryMan,

Normally one would use ExecuteReader for SELECT statements, which you seem to be doing, and call sqlDataReader.RecordsAffected to get the number of rows affected.

Getting -1 from ExecuteNonQuery for a SELECT statement is documented behavior:

"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1." --http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(VS.71).aspx

As for passing parameters to a stored proceudre with ExecuteNonQuery, that should not affect the return value. ExecuteNonQuery does indeed return the number of rows affected, just not for SELECT statements for example.

Here's some sample code for using SqlDataReader to get the number of rows affected from a SELECT Statement:

SqlCommand selectStatement =newSqlCommand();

selectStatement.CommandText ="PROC_SELECT_ITEM";

selectStatement.CommandType =CommandType.StoredProcedure;

selectStatement.Connection = connection;

SqlDataReader reader = selectStatement.ExecuteReader();

int rowsAffected = reader.RecordsAffected;

Pete

Hi Pete.

What you wrote is correct, but I do not think it will work if the stored procedure has a SET NOCOUNT OFF?

Thanks.

|||

Hi CS4Ever,

Thanks. You are right, you cannot have the SET NOCOUNT in the stored procedure.

In any case, you do not typically use ExecuteNonQuery to execute a SELECT statement, especially if you want to know how many rows are retrieved.

So both suggestions are indeed valid, with the resulting suggestion being to use an ExecuteReader with a stored procedure without the SET NOCOUNT statement.

Pete

|||

Peter Lee:

Hi CS4Ever,

Thanks. You are right, you cannot have the SET NOCOUNT in the stored procedure.

In any case, you do not typically use ExecuteNonQuery to execute a SELECT statement, especially if you want to know how many rows are retrieved.

So both suggestions are indeed valid, with the resulting suggestion being to use an ExecuteReader with a stored procedure without the SET NOCOUNT statement.

Pete

You are right.

Thanks Pete.

Monday, March 12, 2012

Execute SQL Task: Error

I am running a Execute SQL Task which runs a script on a table. It gives me following error:

[Execute SQL Task] Error: There is an invalid number of result bindings returned for the ResultSetType: "ResultSetType_SingleRow".

Thanks,

Your query in the execute sql task is returning more than one row. Pretty much just what the error states.

If you don't need to return the results of the query to a variable, select "none" instead of "single row" in the ResultSet box. If you do in fact need to return more than one row into a variable, you'll need to select "Full Result Set." The variable you populate with single row needs to match (or be able to cast) the data type of the returned value. If you are using "Full Result Set" you'll need to use a variable of "object" data type and then enumerate through that variable using a foreach loop.|||http://msdn2.microsoft.com/en-us/library/ms141003.aspx

Friday, March 9, 2012

Execute SQL Task - Assign the result to a variable

Hi,

Let's say that the query in my SQL Task returns a single integer number.

How can I put that single number in a variable?

Thank you.

You should read through Books On-Line for SSIS.

Here's another resource for the Execute SQL Task: http://www.sqlis.com/58.aspx

|||Thank you.

Wednesday, March 7, 2012

Execute SQL based on number rows in two tables

I have a table that I would like to backup each morning only if the number o
f
records in this table is greater than in another table. How can achieve
this? Basically, I would like it to be like this:
IF (rowcount in A) > (rowcount in B) then
Execute SQL
ELSE --NOTHING
END IF"examnotes" <Pasha@.discussions.microsoft.com> wrote in
news:DA8B4552-A94A-4974-9C4A-9A639CC7C1EC@.microsoft.com:

> I have a table that I would like to backup each morning only if the
> number of records in this table is greater than in another table. How
> can achieve this? Basically, I would like it to be like this:
> IF (rowcount in A) > (rowcount in B) then
> Execute SQL
> ELSE --NOTHING
> END IF
Something like this?
if ((select count(*) from sys.tables) > (select count(*) from sys.views))
begin
print 'More tables than views'
end
Ole Kristian Bangs
MCT, MCDBA, MCDST, MCSE:Security, MCSE:Messaging|||Below is what I was able to create. Not sure if it is the best solution
though:
if ((select rows FROM sysindexes WHERE id = OBJECT_ID('Daily_Fin_Apps') AND
indid < 2) > (select rows FROM sysindexes WHERE id =
OBJECT_ID('BACKUP_Daily_Fin_Apps') AND indid < 2))
begin
truncate table [BACKUP_Daily_Fin_Apps]
insert into [BACKUP_Daily_Fin_Apps]
select * from [Daily_Fin_Apps]
end
Thanks,
Pasha
"Ole Kristian Bang?s" wrote:

> "examnotes" <Pasha@.discussions.microsoft.com> wrote in
> news:DA8B4552-A94A-4974-9C4A-9A639CC7C1EC@.microsoft.com:
>
> Something like this?
> if ((select count(*) from sys.tables) > (select count(*) from sys.views))
> begin
> print 'More tables than views'
> end
> --
> Ole Kristian Bang?s
> MCT, MCDBA, MCDST, MCSE:Security, MCSE:Messaging
>

Sunday, February 19, 2012

Execute Package Task

I am using many Execute Package Tasks in a DTS packages to call a number of other DTS packages. The problem is that when I change one of the called DTS packages the execute package task does not run the updated packages. It looks like the execute package task references a Package ID guid instead of the name. Is there a way to make it so a change to a called DTS package will reflect in all references to it

Hi James,

I'd like to know whether you're using the DTS in SQL Server 2000 or SSIS in SQL Server 2005.

If the DTS package is on the same machine as it is running, it has to be fine. The execute package task references the package according to its name.

If you're working on SSIS in SQL Server 2005, please confirm if you have uploaded it to the server. As when you have modified it, it still remains on the client side.

|||

Kevin, thanks for the response. I am using DTS and the package is on the same server. Though it is definatley referencing it by PackageID. If I use the DTSRun utility I can reference it by name. However, when use execute package inside another package it references it by PackageID

Friday, February 17, 2012

Execute Maximum Number (that varies each month) of Concurrent Running Executables

I know that if I set the Package.MaxConcurrentExecutables Property to -1, it will execute the maximum number of concurrent running executables to equal the number of processors plus two.

I have a requirement where I have to run the maximum number of concurrent running executables. However, the number of concurrent executables to run varies each month (it is not fixed).

If I have a loop inside the package that executes each stored procedure (same sproc, different parameter), will SQL Server execute the maximum number of concurrent running executables possible? (It may execute 4 or 8 at a time and that is fine.)

Thanks,

Michael

I'm a little confused about the requiremnt, and if I understood it better, I might be able to suggest a workaround. What's the scenario that drives this requirement?

The answer to your question is no, the Loop does not perform parallel executions (not yet, at least - hoping for support in Katmai). If you are launching an asynchronous process (a process that returns a result immediately, then continues doing the work), you can have multiple processes running in parallel. However, the Execute SQL Task is not asynchronous, so you'd have to be doing something in your stored procedure to enable an asynchronous operation.

Execute Job from another Job based on failure

I have two jobs A and B. I want to execute Job B is job
A fails but only if the error number is 3013. How can I
make this happen?
Thanks,
Vichow about sp_start_job?
"Vic" <vduran@.specpro-inc.com> wrote in message
news:e9fa01c3f183$e79f4350$a101280a@.phx.gbl...
> I have two jobs A and B. I want to execute Job B is job
> A fails but only if the error number is 3013. How can I
> make this happen?
> Thanks,
> Vic

Execute Job from another Job based on failure

I have two jobs A and B. I want to execute Job B is job
A fails but only if the error number is 3013. How can I
make this happen?
Thanks,
Vichow about sp_start_job?
"Vic" <vduran@.specpro-inc.com> wrote in message
news:e9fa01c3f183$e79f4350$a101280a@.phx.gbl...
> I have two jobs A and B. I want to execute Job B is job
> A fails but only if the error number is 3013. How can I
> make this happen?
> Thanks,
> Vic