Showing posts with label via. Show all posts
Showing posts with label via. Show all posts

Friday, March 23, 2012

Getting SQL Server deadlock error - how do I work around?

I have some ASP.NET C# code which executes a stored procedure in SQL Server via the SqlCommand and SqlConnection classes.

One of the stored procedures that gets executed is giving the error: "Transaction (Process ID 272) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction." This only happens occassionally.

Is there a way to get around this in my ASP.Net application? One thing I tried is ensuring that no 2 users entered the stored procedure concurrently:

object synclock =new object() ;lock (synclock) {// execute SQL stored procedure ...}
This did not solve the problem, and I'm not even sure if that is the correct implementation to ensure sequential execution of the stored procedure.

I use a method which some frown upon but if you can handle dirty reads you will be fine. (sql 2000 SP4 or sql 2005) - use the no lock table hint on all your selects and joins in the procedure.

for example: select field1... from table1 with (NOLOCK) on joins its ... inner join table2 with (nolock) ...

So this will ask sql to read the data even if its in flux but if you don't care it will help with dead locks.

-c

Monday, March 19, 2012

getting recordset via ado/sproc

Trying to get output from sqlserver stored procedure into access recordset via ado.

I have stored procedure with select statement that returns rows when executed from query analyser:

CREATE procedure ps_tblMackVendors_Select_test
AS
set quoted_identifier on
declare @.strsql nvarchar(300)
set @.strsql = 'Select * from tblMackVendors
exec (@.strsql)
GO

When I execute following bit of code from Access, the set rst = cmd.execute runs ok, but the rst.movefirst gets error 3704, Operation is not allowed when the object is closed.

Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = mcnnSQLDB
cmd.CommandText = "ps_tblMackVendors_Select_test"
cmd.CommandType = adCmdStoredProc
Set rst = cmd.Execute
rst.MoveFirst

I have tested the stored procedure & the connection using an output parameter, doing a cmd.execute (without the rst..) & the value is retrieved by access okprobably you need to rethink your app structure before you go too far with it. i bet your rst will successfully movefirst if you just do a straight select from your sp.|||My question would be why do you have

CREATE procedure ps_tblMackVendors_Select_test
AS
set quoted_identifier on
declare @.strsql nvarchar(300)
set @.strsql = 'Select * from tblMackVendors
exec (@.strsql)
GO

instead of,...

CREATE procedure ps_tblMackVendors_Select_test
AS
Select * from tblMackVendors
GO

???|||well, i understand he's just experimenting, getting ready for the real stuff, but i'm saying "don't go that route, there is a sign there - no outlet!"|||What happens if you open a recordset using a sql string instead of a stored procedure ? Also, you should debug your vb code and view the locals to see what is going on with the rst object.|||my sp uses string for qry because I want to build the sql from values passed to the sp in the final version.
I did just try it without the string for the sql & it worked - good idea. But it does work with the string when I call the sp from query analyzer. Why do you think there is a difference ?|||multiple recordsets is one option,...

using sp_executesql instead of exec might help.

personally I'd really avoid this method of doing things though... if you are going to use dynamic sql then use it in the application, not in a stored proc.

Monday, March 12, 2012

getting primary key out of insert into statement

Is there a way to get the primary key that is causing a primary key violation when inserting via a insert into with a select statement. This violation is being caught using a try/catch statement. Is there anyway, short of doing yourself via brute force coding, to get what the values are that are causing the error. Basically, I'm doing:

begin try

insert into TableA

select col1, col2, col3

from TableB

end try

begin catch

--Catch occasional primary key violation.

end catch

not sure if this is what you want to do, but have you considered something like the following?

lets say you have a schema like this (psuedo metadata for brevity sake) and you want to insert table1 into table2


[NB: may have errors as I didn't run this through QA, but the concept is sound]

legend: TableName , primary key fields

table1
A1
A2
A3

table2
B1
B2
B3

then the table1 records that will be a violation can be selected via

select distinct table1.* into #violations
from table1 inner join table2
on table1.A1 = table2.B1
and table1.A2 = table2.B2

Then you can insert the valid records via:

insert into table2
select table1.*
from table1 left join table2
on table1.A1 = table2.B1
and table1.A2 = table2.B2
where table2.B1 is null

then, you can do

select * from #violations

and do what ever you want with these - write to another table/send in an email, etc. . .

lastly you will want to delete the temporary #violations table

drop table #violations

|||Check out the OUTPUT option of the INSERT statement.

getting one instance of multiple records via query

I'm a longtime Access db developer just now really getting into MSDE and
SQL Server 2000.
I have an Access SQL query that retrieves only the first instance of any
date value from a series of records. This gives me a simple list of
available date values fro a dropdown combo box.
My query works great in an Access MDB, but apparently there is no First()
function in SQL Server and I can't find an analog. SQL Server will not
import and reconfigure my queries as it did my tables.
The Access SQL is as follows:
SELECT First(OrderSetupMaster.OrderDate) AS [Order Date Field]
FROM OrderSetupMaster
GROUP BY OrderSetupMaster.OrderDate
ORDER BY First(OrderSetupMaster.OrderDate);
Any suggestions?
Thanks,
Randall Arnold
On Fri, 14 Oct 2005 15:37:58 -0700, divineblasphemy@.hotmail.com wrote:

>I'm a longtime Access db developer just now really getting into MSDE and
>SQL Server 2000.
>I have an Access SQL query that retrieves only the first instance of any
>date value from a series of records. This gives me a simple list of
>available date values fro a dropdown combo box.
>My query works great in an Access MDB, but apparently there is no First()
>function in SQL Server and I can't find an analog. SQL Server will not
>import and reconfigure my queries as it did my tables.
>The Access SQL is as follows:
>SELECT First(OrderSetupMaster.OrderDate) AS [Order Date Field]
>FROM OrderSetupMaster
>GROUP BY OrderSetupMaster.OrderDate
>ORDER BY First(OrderSetupMaster.OrderDate);
>Any suggestions?
>Thanks,
>Randall Arnold
Hi Randall,
FIRST() is very non-relational in nature. By definition, the rows in a
relational table are UNordered, so there is no first row.
If you want the oldest OrderDate, try MIN(OrderSetupMaster.OrderDate).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Won't that just give me a single row? I need multiple rows, if there are
multiple date values. So if my recordset dates are, say, 12/10/2004
12/11/2004 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004
12/11/2004 12/13/2004... not just a MIN of 12/10/2004 or MAX of
12/13/2004.
Make sense?
Randall Arnold
|||Randal,
Based on what criteria (grouping)? Perhaps you are looking for DISTINCT instead?
i.e.,
SELECT DISTINCT(ORDERDATE)AS [Order Date Field]
FROM ORDERSETUPMASTER
ORDER BY ORDERDATE
HTH
Jerry
<randall.arnold@.nokia.com> wrote in message news:u1nOsaR0FHA.3924@.TK2MSFTNGP14.phx.gbl...
Won't that just give me a single row? I need multiple rows, if there are multiple date values. So if my recordset dates are, say, 12/10/2004 12/11/2004 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004 12/11/2004 12/13/2004... not just a MIN of 12/10/2004 or MAX of 12/13/2004.
Make sense?
Randall Arnold
|||Or...
with the following data in the table:
1 2004-12-10 00:00:00.000
2 2004-12-12 00:00:00.000
2 2004-12-11 00:00:00.000
2 2004-12-12 00:00:00.000
3 2004-12-12 00:00:00.000
3 2004-12-11 00:00:00.000
4 2004-12-11 00:00:00.000
and the query:
SELECT ID, MIN(ORDERDATE)
FROM ORDERSETUPMASTER
GROUP BY ID
ORDER BY MIN(ORDERDATE)
and the resultset:
1 2004-12-10 00:00:00.000
2 2004-12-11 00:00:00.000
3 2004-12-11 00:00:00.000
4 2004-12-11 00:00:00.000
HTH
Jerry
"Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message news:OZmAUfR0FHA.2076@.TK2MSFTNGP14.phx.gbl...
Randal,
Based on what criteria (grouping)? Perhaps you are looking for DISTINCT instead?
i.e.,
SELECT DISTINCT(ORDERDATE)AS [Order Date Field]
FROM ORDERSETUPMASTER
ORDER BY ORDERDATE
HTH
Jerry
<randall.arnold@.nokia.com> wrote in message news:u1nOsaR0FHA.3924@.TK2MSFTNGP14.phx.gbl...
Won't that just give me a single row? I need multiple rows, if there are multiple date values. So if my recordset dates are, say, 12/10/2004 12/11/2004 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004 12/11/2004 12/13/2004... not just a MIN of 12/10/2004 or MAX of 12/13/2004.
Make sense?
Randall Arnold
|||Distinct or Group by will be enough
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/
|||Yeah, stupid me, DISTINCT did the trick. That occurred to me before I
even saw your post and I was cringing at the prospect of what responses
would look like today...lol.
Thanks,
Randall

getting one instance of multiple records via query

I'm a longtime Access db developer just now really getting into MSDE and
SQL Server 2000.
I have an Access SQL query that retrieves only the first instance of any
date value from a series of records. This gives me a simple list of
available date values fro a dropdown combo box.
My query works great in an Access MDB, but apparently there is no First()
function in SQL Server and I can't find an analog. SQL Server will not
import and reconfigure my queries as it did my tables.
The Access SQL is as follows:
SELECT First(OrderSetupMaster.OrderDate) AS [Order Date Field]
FROM OrderSetupMaster
GROUP BY OrderSetupMaster.OrderDate
ORDER BY First(OrderSetupMaster.OrderDate);
Any suggestions?
Thanks,
Randall ArnoldOn Fri, 14 Oct 2005 15:37:58 -0700, divineblasphemy@.hotmail.com wrote:

>I'm a longtime Access db developer just now really getting into MSDE and
>SQL Server 2000.
>I have an Access SQL query that retrieves only the first instance of any
>date value from a series of records. This gives me a simple list of
>available date values fro a dropdown combo box.
>My query works great in an Access MDB, but apparently there is no First()
>function in SQL Server and I can't find an analog. SQL Server will not
>import and reconfigure my queries as it did my tables.
>The Access SQL is as follows:
>SELECT First(OrderSetupMaster.OrderDate) AS [Order Date Field]
>FROM OrderSetupMaster
>GROUP BY OrderSetupMaster.OrderDate
>ORDER BY First(OrderSetupMaster.OrderDate);
>Any suggestions?
>Thanks,
>Randall Arnold
Hi Randall,
FIRST() is very non-relational in nature. By definition, the rows in a
relational table are UNordered, so there is no first row.
If you want the oldest OrderDate, try MIN(OrderSetupMaster.OrderDate).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Won't that just give me a single row? I need multiple rows, if there are
multiple date values. So if my recordset dates are, say, 12/10/2004
12/11/2004 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004
12/11/2004 12/13/2004... not just a MIN of 12/10/2004 or MAX of
12/13/2004.
Make sense?
Randall Arnold|||Randal,
Based on what criteria (grouping)? Perhaps you are looking for DISTINCT ins
tead?
i.e.,
SELECT DISTINCT(ORDERDATE)AS [Order Date Field]
FROM ORDERSETUPMASTER
ORDER BY ORDERDATE
HTH
Jerry
<randall.arnold@.nokia.com> wrote in message news:u1nOsaR0FHA.3924@.TK2MSFTNGP
14.phx.gbl...
Won't that just give me a single row? I need multiple rows, if there are mu
ltiple date values. So if my recordset dates are, say, 12/10/2004 12/11/200
4 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004 12/11/2004 12/1
3/2004... not just a MIN of 12/10/2004 or MAX of 12/13/2004.
Make sense?
Randall Arnold|||Or...
with the following data in the table:
1 2004-12-10 00:00:00.000
2 2004-12-12 00:00:00.000
2 2004-12-11 00:00:00.000
2 2004-12-12 00:00:00.000
3 2004-12-12 00:00:00.000
3 2004-12-11 00:00:00.000
4 2004-12-11 00:00:00.000
and the query:
SELECT ID, MIN(ORDERDATE)
FROM ORDERSETUPMASTER
GROUP BY ID
ORDER BY MIN(ORDERDATE)
and the resultset:
1 2004-12-10 00:00:00.000
2 2004-12-11 00:00:00.000
3 2004-12-11 00:00:00.000
4 2004-12-11 00:00:00.000
HTH
Jerry
"Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message news:OZmAUfR0FHA.20
76@.TK2MSFTNGP14.phx.gbl...
Randal,
Based on what criteria (grouping)? Perhaps you are looking for DISTINCT ins
tead?
i.e.,
SELECT DISTINCT(ORDERDATE)AS [Order Date Field]
FROM ORDERSETUPMASTER
ORDER BY ORDERDATE
HTH
Jerry
<randall.arnold@.nokia.com> wrote in message news:u1nOsaR0FHA.3924@.TK2MSFTNGP
14.phx.gbl...
Won't that just give me a single row? I need multiple rows, if there are mu
ltiple date values. So if my recordset dates are, say, 12/10/2004 12/11/200
4 12/11/2004 12/13/2004, then I expect to pull up 12/10/2004 12/11/2004 12/1
3/2004... not just a MIN of 12/10/2004 or MAX of 12/13/2004.
Make sense?
Randall Arnold|||Distinct or Group by will be enough
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/|||Yeah, stupid me, DISTINCT did the trick. That occurred to me before I
even saw your post and I was cringing at the prospect of what responses
would look like today...lol.
Thanks,
Randall

Wednesday, March 7, 2012

Getting messages sent while JDBC Driver calls stored procedure

Hi.

How can I get the messages sent by the server while I'm executing a stored procedure via the JDBC driver?

I need to get my own debug messages (done through the print() function)

and also standard server messages (such as "x row(s) affected" or

results from SET STATISTICS TIME ON). Is this possible?

Many thanks.

Carlos

Hi Carlos,

These messages come back as warnings. You need to call Statement.getWarnings to retrieve them. See http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#getWarnings() for usage.

Hope this helps.

--David Olix

JDBC Development

|||Hi David.

That didn't work for me. I am using SQL Server 2005 JDBC driver version 1.1, and here's my calling syntax:
SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();

//(sic) set the datasource parameters...

Connection c = c.getPooledConnection().getConnection();

CallableStatement cs = c.prepareCall("{call myStoredProcedure(?,?)");

//(sic) set the stored procedure parameters...


ResultSet rs = cs.executeQuery();
SQLWarning w = cs.getWarnings();
while( w!=null ){

System.out.println(w.getMessage());

w=w.getNextWarning()

}

When debugging, I see that the first call to

cs.getWarnings() returns a null value. I also tryed rs.getWarnings()

and c.getWarnings(), and both returned null as well. When calling the

same stored

procedure through QueryAnalyser, all the messages are there.

Any ideas on what might be causing this?

Thx,

Carlos

|||

What you have should have worked if the warning message appeared before the result set. If the message came after the result set, however, you would need to process the rows in the result set before the messages can be extracted with cs.getWarnings. To be sure that you're getting through all of the results, including any warnings, you should call Statement.getMoreResults until there are no more results. See http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#getMoreResults() for how.

Thanks,

--David