Creating 2nd transaction form inside db ?

MS SQL SERVER

    Next

  • 1. Autoincrementing a column for rows with null
    Is there a way, in a Select statement, to 'auto-increment' a column with numbers to only affect the rows with nulls in them? Obviously, this will not work... select col1 = identity(int,1,1) .. because some rows have values. I do not need to worry about what the value is, they just need to 'count off'. Jason
  • 2. SQL Out Mail in Inbox
    Maybe you would like to post your code? Then someone could review and post a suggestion. Otherwise, unless someone has experienced exactly what you are, noone will reply... HTH JeffP.... "M.Blake" < XXXX@XXXXX.COM > wrote in message news:69f601c340c8$4b5f9ce0$ XXXX@XXXXX.COM ... > I am trying to automatically send mail from within a > stored procedure. Everything seems(?) to be setup as > needed and working(?) correctly but the outbound messages > are winding up in Outlook's Inbox! > > The FROM: address shows SQL Server and the TO: shows the > intended target. The only way I've been able to get the > message to actually fly is to manually open the message > from within Outlook and click the Send button. This is > defeating the whole idea of automatic messages. > > Any and all help is appreciated. Thanx in advance.
  • 3. How stop a xp_cmdshell process ???
    Hello, Someone knows the way to stop a process genetared by command : EXEC master..xp_cmdshell @PsCMD For example: I have this code to check pingable: Set @LsIPAddress = '56.87.197.25'; Set @PsCMD = 'ping ' + convert(varchar,@LsIPAddress) + ' - t'; Insert into LogPing(Status) EXEC master..xp_cmdshell @PsCMD PRINT 'Process successful!' But this continue doing it forever which it is ok. But I just need to run it for 10 seconds and then stop it. How I can do that? it is possible ? Any help is greatly appreciated.

Creating 2nd transaction form inside db ?

Postby Adrian Parker » Tue, 05 Sep 2006 20:47:41 GMT

We have a table that holds last inserted key values for several tables. 
These keys are alphanumeric with a fixed format, so we can't just do Max +1 
on them.   So we have a procedure that gets the current key, increments it 
as necessary, updates the table and returns the key to the caller.. be it 
from TSQL or from app code.      This is fine when called from app code as 
we can control the connection used for the call so we don't get lock 
problems, but from TSQL it's currently a problem as it resides inside the 
transaction of the caller, so the updates don't happen until the caller 
transaction is complete.

Is there any way of telling the DB to use an atominc operation for a 
procedure ?  i.e. take a copy of the current connection and use the copy to 
perform an operation ?

Thanks.
-- 
Adrian Parker
Ingenuity At Work Ltd 



Re: Creating 2nd transaction form inside db ?

Postby Erland Sommarskog » Wed, 06 Sep 2006 07:33:08 GMT

Adrian Parker ( XXXX@XXXXX.COM ) writes:

Not really correct. The update does happen, but row remains locked until 
the transaction commits. (Of course, if other processes that read this 
table, uses snapshot isolation, they will see the old value.)


The only way to do that is to have a loopback operation. That is, the
procedure would call a CLR procedure that makes a second connection to
retrieve the key. If you are using SQL 2000, that would have to be an
extended stored procedure.

I need to add that this is a solution that I want in no way to encourage.
It is not likely that it will scale well. There is also a question, what if 
a caller rolls back after having a got a value. Do you accept that that 
key will not be used?

-- 
Erland Sommarskog, SQL Server MVP,  XXXX@XXXXX.COM 

Books Online for SQL Server 2005 at
 http://www.**--****.com/ 
Books Online for SQL Server 2000 at
 http://www.**--****.com/ 

Re: Creating 2nd transaction form inside db ?

Postby Adrian Parker » Wed, 06 Sep 2006 16:47:17 GMT

Erland, thanks for replying..






| Not really correct. The update does happen, but row remains locked until
| the transaction commits. (Of course, if other processes that read this
| table, uses snapshot isolation, they will see the old value.)

What I mean is that yes, it happens, but as we use read committed, no other 
process sees the updated value.  Also, some transactions are quite long and 
would cause horrible waits for the locks to be cleared.   Currently, we have 
to pre-allocate a range of keys to use inside the trans, but that is 
wasteful.

| The only way to do that is to have a loopback operation. That is, the
| procedure would call a CLR procedure that makes a second connection to
| retrieve the key. If you are using SQL 2000, that would have to be an
| extended stored procedure.

We're using 2000,  how do you create a second connection ?

Thanks
Adrian



Re: Creating 2nd transaction form inside db ?

Postby changliw » Wed, 06 Sep 2006 22:39:26 GMT

Hi Adrian,
I'm not sure I better understand your issue, but how about the following 
structure:
DECLARE @vId varchar(20)
exec proc_getID(@vId)
Go
BEGIN TRAN
..
..
COMMIT TRAN

If all the statements are in one transaction, you can reference the 
following:
BEGIN TRAN
DECLARE @vId varchar(20)
exec proc_getID(@vId)

SAVE TRAN pt_rollback
..
..
--If it's necessary to rollback, you can call
ROLLBACK TRAN pt_rollback
COMMIT TRAN

Anyway, I'm interested in your control of your app. Could you tell me more 
about the logic and what is your intent to get the ID by starting a new 
connection?

If you want to ensure the ID's unique value, you can set the transaction 
Serializable.

Sincerely,
Charles Wang
Microsoft Online Community Support


Re: Creating 2nd transaction form inside db ?

Postby Alexander Kuznetsov » Wed, 06 Sep 2006 22:47:55 GMT

> would cause horrible waits for the locks to be cleared.   Currently, we have
Adrian,

Why is it wasteful? Anyway, it sounds like message queues do exactly
what you are looking for. If you are on SQl Server 2005, use the
Broker.
Makes sense?


Re: Creating 2nd transaction form inside db ?

Postby Adrian Parker » Thu, 07 Sep 2006 04:47:54 GMT

Charles,

We have transactions that can take several seconds to run (they're doing a 
lot of work).

Each transaction performs inserts into several tables, some of which 
required the production of an alphanumeric key.  These keys are held in a 
key_allocation table which holds the last key assigned, so for example 
TR000123 would be incremented to TR000124 and returned to the caller.

Because the transactions can be long, we don't want to call the routine to 
get the next key and leave that key_allocation record in an uncommited state 
as that would stop other processes from getting other keys from that record 
for inserts into their own transactions.  Because we don't know how many 
keys to obtain before we start the transaction, we would have to 
pre-allocate a lot keys to ensure we didn't run out during the transaction, 
which would be expensive on available keys.

begin tran
    get key1
    insert record into table1
    get key2
    insert record into table2
    lots of other work
commit

So by the time we got to the comit, several seconds could have gone by with 
key1 and key2 records still locked.

-Adrian






| Hi Adrian,
| I'm not sure I better understand your issue, but how about the following
| structure:
| DECLARE @vId varchar(20)
| exec proc_getID(@vId)
| Go
| BEGIN TRAN
| ..
| ..
| COMMIT TRAN
|
| If all the statements are in one transaction, you can reference the
| following:
| BEGIN TRAN
| DECLARE @vId varchar(20)
| exec proc_getID(@vId)
|
| SAVE TRAN pt_rollback
| ..
| ..
| --If it's necessary to rollback, you can call
| ROLLBACK TRAN pt_rollback
| COMMIT TRAN
|
| Anyway, I'm interested in your control of your app. Could you tell me more
| about the logic and what is your intent to get the ID by starting a new
| connection?
|
| If you want to ensure the ID's unique value, you can set the transaction
| Serializable.
|
| Sincerely,
| Charles Wang
| Microsoft Online Community Support
| 



Re: Creating 2nd transaction form inside db ?

Postby Erland Sommarskog » Thu, 07 Sep 2006 07:23:32 GMT

Adrian Parker ( XXXX@XXXXX.COM ) writes:


As I said, you write an extended stored procedure (XP) which reconnects
to SQL Server. But XPs are not for the faint of heart. You have to write
them in C++ and what's really bad: if you XP crash because of for instance
an access violation, this brings down the entire SQL Server. So this is
definitely a way that I would encourage.

One alternative would be to pre-generate lots of keys, and any process
that needs a key would do something like:

   SELECT MIN(keyvalue)
   FROM   tbl WITH (READPAST)
   WHERE  takenby IS NULL

I think a better option would be to look into is to have seperate
series for the batch-oriented stuff.

-- 
Erland Sommarskog, SQL Server MVP,  XXXX@XXXXX.COM 

Books Online for SQL Server 2005 at
 http://www.**--****.com/ 
Books Online for SQL Server 2000 at
 http://www.**--****.com/ 

Re: Creating 2nd transaction form inside db ?

Postby Adrian Parker » Thu, 07 Sep 2006 07:33:29 GMT




| Adrian Parker ( XXXX@XXXXX.COM ) writes:
| >| The only way to do that is to have a loopback operation. That is, the
| >| procedure would call a CLR procedure that makes a second connection to
| >| retrieve the key. If you are using SQL 2000, that would have to be an
| >| extended stored procedure.
| >
| > We're using 2000,  how do you create a second connection ?
|
|
| As I said, you write an extended stored procedure (XP) which reconnects
| to SQL Server. But XPs are not for the faint of heart. You have to write
| them in C++ and what's really bad: if you XP crash because of for instance
| an access violation, this brings down the entire SQL Server. So this is
| definitely a way that I would encourage.
|
| One alternative would be to pre-generate lots of keys, and any process
| that needs a key would do something like:
|
|   SELECT MIN(keyvalue)
|   FROM   tbl WITH (READPAST)
|   WHERE  takenby IS NULL
|
| I think a better option would be to look into is to have seperate
| series for the batch-oriented stuff.

Hmm.. I think we're going to have to bite the bullet and change the keys to 
use identity cols (huge job).

We have no idea how many new records are going to be created during some of 
the transactions, it could be a few or it could be thousands.. it depends on 
how complex the varying data is.

Thanks for trying to help !
-Adrian






Re: Creating 2nd transaction form inside db ?

Postby Roger Wolter[MSFT] » Thu, 07 Sep 2006 09:10:29 GMT

I did this in a couple of applications with a routine in my application that 
handed out numbers as they were required.  This routine would get a bunch of 
numbers from the "LastNumberUsed" table by incrementing the value by some 
amount (I used 100 but made it tunable).  When the internal count reached 
the number in the database, I got 100 more.  Since only one thread actually 
updated the database, there were no contention issues.   Because this 
routine wasn't part of any transaction, there was no blocking.  The routine 
obviously used a critical section to deal with multi-threading issues but 
since it just incremented a number and returned it, there wasn't much 
overhead.  When the application shut down, I set the "LastNumberUsed" table 
to the last number I really used.  If the application crashed, I lost up to 
100 numbers but That wasn't an issue for me.  Identity wouldn't have worked 
for me but that's another story.  I don't know if this helps but I thought I 
would throw it in.

-- 
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
 http://www.**--****.com/ 












RE: Creating 2nd transaction form inside db ?

Postby changliw » Tue, 12 Sep 2006 22:08:05 GMT

Dear Adrian,
Thanks for your email response. I got an idea and hope it's helpful to you.

This method requires a table to store the generated IDs. The table's 
structure is as following:
CREATE TABLE TheTable
(
  TheID int,                           -- computed value by your business 
rules
  TheRequestID uniqueidentifier  -- a random unique number representing 
each request
)

Assume that you have a stored procedure "addid" to generate the ID.
 
Then you can create a procedure for getting the ID like:
Create procedure usp_getID
 (@requestId uniqueidentifier, --represent a unique request 
@nId int output)
AS
declare @vstrAddId varchar(200)
SET @vstrAddId = 'osql /Sinstancename /E /Q"USE <databasename>;exec addid 
@requestno=''' + requestId + ''';"'
Exec xp_cmdshell @vstrAddId
Select @nId = TheID from TheTable Where TheRequestId = @requestId
DELETE TheTable WHERE TheRequestId=@requestId

If you have any other questions or concerns, please feel free to let me 
know. It's my pleasure to be of assistance.

Sincerely,
Charles Wang
Microsoft Online Community Support


Similar Threads:

1.CREATE DATABASE inside a transaction with Microsoft SQL 2005

Hi,
Till now I am using SQL server 2000 with Microsoft JDBC SP3 for 2000.
Everything is fine.
But now I moved to SQLServer 2005 with Microsoft JDBC for 2005. But it
throws
an error "CREATE DATABASE statement not allowed within multi-statement
transaction" when our application tries to create a new DB inside a
transaction. All other DDL statements like CREATE TABLE, ALTER etc
works well inside a transaction with JDBC. We found that only CREATE

DATABASE has problem.
This problem is not there with Old JDBC driver with SQL 2000.
I know such a  problem is there with SYBASE in which by default sybase
wn't allow DDL statements inside a transaction. But they provide an
option DDL_IN_TRAN or something like that. Enabling this will allow
DDL.


How I can make CREATE DATBASE work inside a transaction. Is there any
options available. 


Regards 
zunilp 
Cordys

2.The sql transaction sample in MSDN is so weird, only one command inside the whole transaction

3.Create online form to feed in SQL DB

Hi:

 I have been tasked with creating an online course evaluation form.  This form 
will be used by our client students to give us feedback on the courses they 
take with us.

 I have two needs:

 ONSITE - I need a good tutorial on how to create a form in Flash that will 
populate a SQL DB.  Students will complete the form when onsite at our HQ.  
This form will be on our intranet and will directly feed the SQL DB.

 OFFSITE - OK.  This is the more challenging issue.  Our trainers tell us that 
many of our clients (believe it or not) do not have or allow internet access to 
the PCs used for training at their site.  So, the students cannot use our 
online form (even though we could put it on the internet).

 Can I modify the Flash form used for onsite, for the offsite?  The trainer 
would install the file and the Flash plug-in if needed on the client's PC.  The 
student would complete the form and the Flash form would store their responses 
to their local PC.  The trainer would then copy the output file to a jump 
drive.  When the trainer returns to HQ, he/she would need to be able to import 
those responses to the SQL DB.

 How can I do this and make it relatively simple for the trainer?

 Thanks,
 Loren

4.normalization question about 2nd normal form

Chris,

  In your design, the following data is allowed:

Insert into Drivers values (1, 'Bill', 'Clinton', '000-000-0000')
Insert into Drivers values (2, 'Hillary', 'Clinton', '000-000-0000')
Insert into DriversDependants values (1,1,'Clinton', 'Chelsea', 27, 'F')
Insert into DriversDependants values (2,1,'Clinton', 'Chelsea', 27, 'F')

So is the following:

Insert into Drivers values (1, 'Bill', 'Clinton', '000-000-0000')
Insert into Drivers values (2, 'Hillary', 'Clinton', '000-000-0000')
Insert into DriversDependants values (1,1,'Clinton', 'Chelsea', 27, 'F')
Insert into DriversDependants values (2,1,'Clifton', 'Chelcee', 127, 'F')

Whether your model is 2NF depends on whether you want
to allow the second situation, and whether the two Chelsea Clintons
in the first group of inserts are different people (or should be considered
different in your business).

If DependantID values can be repeated so long as they
are repeated for distinct DriverID values, and if you don't
care about the possibility of two drivers having the same
dependent (you consider them different regardless), you model
is ok.  (DependantID 1 means "first for this driver", for example).

If you want to be able to refer to a dependent independently
of a driver, then this model won't work for you.  Either it duplicates
facts (as in the first group of inserts), threatening data integrity,
or it creates ambiguous situations (who is DependantID = 1 ?)

By the way, if all the dependent information depends on the
DependantID value, 2NF requires moving last name and first
name to the table of dependents also, not just age and sex.

-- Steve Kass
-- Drew University
-- Ref: A68CFE10-6F8A-48F3-8E0F-8BA1142735B5




chris wrote:

>Im reading an old book I have about DB desgin and it 
>brought up a good point. Take these tables:
>
>Drivers
>------------
>DriverID(PK)
>FName
>LName
>SSN
>
>
>DriversDependants
>----------------------
>DriverID(FK)(PK)
>DependantID(PK)
>DependantLName
>DependantFName
>DependantAge
>DependantSex
>
>
>The book says that to be in 2NF you should split the 
>DriversDependants table into 2 tables. The end result 
>should be:
>
>
>
>Drivers
>------------
>DriverID(PK)
>FName
>LName
>SSN
>
>
>Dependants
>----------------------
>DriverID(FK)
>DependantID(FK)
>DependantLName
>DependantFName
>
>
>DriversDependants
>--------------------
>DependantID(PK)
>DependantAge
>DependantSex
>
>
>I understand that from a purely relational point of view 
>this is how it should be. But from a more down to earth 
>point of view Im trying to figure out how this would be of 
>any possible value? It seems to me that Age and Sex would 
>always be required so to put them in there own table would 
>only slow things down. I am far from a relational expert 
>so I thought I would see what you guys think. Would there 
>be some benifit here that Im not seeing? Would you do it 
>the way the book says?
>
>Any input would be great.
>Chris
>  
>

5.Create a job to check db transaction log size

I would like to create a tsql script that would check the 
database transaction log file size hourly.(xp_cmdshell)  
If the log file size is greater than 500 MB then run the 
script listed below to truncate the log.

Please help me with this script.

Thank You,

Mike

USE DATABASE
GO
DBCC SHRINKFILE(starterdb_log, 100)

USE DATABASE
GO
BACKUP LOG DATABASE WITH TRUNCATE_ONLY
						
USE DATABASE
GO
DBCC SHRINKFILE(starterdb_log, 100)

6. Optimizations Job and Shrink DB creating HUGE transaction log file

7. Optimizations Job and Shrink DB creating HUGE transaction log

8. child form inside the page



Return to MS SQL SERVER

 

Who is online

Users browsing this forum: No registered users and 2 guest