Showing posts with label temporary. Show all posts
Showing posts with label temporary. Show all posts

Sunday, February 19, 2012

"Additional information: System error" message

Hello,
I needed to look at some .BCP files, so I thought I'd create a small tool for that.
I decided to create a temporary
DB, attach empty mdf, ldf files with predefined structure and then insert the BCP files, so I can view the data as DB tables .

Here is the procedure I follow:
1) I drop the existing (if exists) DB ("Drop DBName") - to be prepared for the next set of .BCP files.
2) Then I create a new
DB using sp_attach_db,
3) Then I populate the db via "Bulk Insert".

1st time this works just fine, but when I try to execute step 1 (drop DB) after step 3 I get a message that there is still open connection and the DB can't be dropped.

Here I can do two different things:
1) Find the
SPID of the open connection to my DB and KILL the process or
2) Instead of killing the process, set the DB to "SINGLE_USER WITH ROLLBACK IMMEDIATE" and then drop the DB.

In both cases I was able to Drop the DB and attach the new one, however the Bulk Insert (via ExecuteNonQuery() ) for some reason closes the current connection and brakes with the "System Error" message.

I simulated this procedure on Query Analyser and on SQL Server Studio Express, but everything worked just fine there.

The VB code is a little complicated with using app.config to open so many network drives and folders, but if anybody is interested I'll post it.

To make it short, here is the SQL Query procedure (which worked, though):

USE Master
if exists
(select name
from master.dbo.sysdatabases
where has_dbaccess(name)=1 and name='MyDB')
BEGIN
Drop database MyDB
END
exec sp_who2

-- Before the next command I have to manually restore the .ndf, .ldf files, deleted by the "Drop DB" command

sp_attach_db @.dbname = N'MyDB',
@.filename1 = N'D:\CurrSchema\DB_Patient.ndf',
@.filename2 = N'D:\CurrSchema\DB_Definitions.mdf',
@.filename3 = N'D:\CurrSchema\DB_Log.ldf',
@.filename4 = N'D:\CurrSchema\DB_Data.ndf'
exec sp_who2

-- WAITFOR DELAY '00:00:04'
-- need to wait manually here. Waitfor won't work with "USE DB"

USE MyDB
BULK INSERT CAL
FROM '\\srv\D$\CurrFiles\CAL.bcp'
WITH (DATAFILETYPE='native',KEEPIDENTITY,KEEPNULLS)

BULK INSERT CAP
FROM '\\srv\CurrFiles\CAP.bcp'
WITH (DATAFILETYPE='native',KEEPIDENTITY,KEEPNULLS)
exec sp_who2

Has anybody seen something like this?

Thank you.

Haven't seen it personally. However, I suggest you insert a 'go' between each command to seperate them into batches. Also, be sure to change the database to 'master' for your connection before sending/executing 'drop db'.|||

Yes, Thank you - you were right. I did not need the 'GO', but the change to 'master' was the key. I should have been more careful when using somebody else's code - no matter how encapsulated, it's never context-free - in my case I had to add context switching with 'USE master'.

George.

|||

Hi... you mentioned you had the below source sample...

The VB code is a little complicated with using app.config to open so many network drives and folders, but if anybody is interested I'll post it.

Can you please post or sent to billbbellevue@.hotmail.com

Kind regards,

Bill Bezick

"Additional information: System error" message

Hello,
I needed to look at some .BCP files, so I thought I'd create a small tool for that.
I decided to create a temporary
DB, attach empty mdf, ldf files with predefined structure and then insert the BCP files, so I can view the data as DB tables .

Here is the procedure I follow:
1) I drop the existing (if exists) DB ("Drop DBName") - to be prepared for the next set of .BCP files.
2) Then I create a new
DB using sp_attach_db,
3) Then I populate the db via "Bulk Insert".

1st time this works just fine, but when I try to execute step 1 (drop DB) after step 3 I get a message that there is still open connection and the DB can't be dropped.

Here I can do two different things:
1) Find the
SPID of the open connection to my DB and KILL the process or
2) Instead of killing the process, set the DB to "SINGLE_USER WITH ROLLBACK IMMEDIATE" and then drop the DB.

In both cases I was able to Drop the DB and attach the new one, however the Bulk Insert (via ExecuteNonQuery() ) for some reason closes the current connection and brakes with the "System Error" message.

I simulated this procedure on Query Analyser and on SQL Server Studio Express, but everything worked just fine there.

The VB code is a little complicated with using app.config to open so many network drives and folders, but if anybody is interested I'll post it.

To make it short, here is the SQL Query procedure (which worked, though):

USE Master
if exists
(select name
from master.dbo.sysdatabases
where has_dbaccess(name)=1 and name='MyDB')
BEGIN
Drop database MyDB
END
exec sp_who2

-- Before the next command I have to manually restore the .ndf, .ldf files, deleted by the "Drop DB" command

sp_attach_db @.dbname = N'MyDB',
@.filename1 = N'D:\CurrSchema\DB_Patient.ndf',
@.filename2 = N'D:\CurrSchema\DB_Definitions.mdf',
@.filename3 = N'D:\CurrSchema\DB_Log.ldf',
@.filename4 = N'D:\CurrSchema\DB_Data.ndf'
exec sp_who2

-- WAITFOR DELAY '00:00:04'
-- need to wait manually here. Waitfor won't work with "USE DB"

USE MyDB
BULK INSERT CAL
FROM '\\srv\D$\CurrFiles\CAL.bcp'
WITH (DATAFILETYPE='native',KEEPIDENTITY,KEEPNULLS)

BULK INSERT CAP
FROM '\\srv\CurrFiles\CAP.bcp'
WITH (DATAFILETYPE='native',KEEPIDENTITY,KEEPNULLS)
exec sp_who2

Has anybody seen something like this?

Thank you.

Haven't seen it personally. However, I suggest you insert a 'go' between each command to seperate them into batches. Also, be sure to change the database to 'master' for your connection before sending/executing 'drop db'.|||

Yes, Thank you - you were right. I did not need the 'GO', but the change to 'master' was the key. I should have been more careful when using somebody else's code - no matter how encapsulated, it's never context-free - in my case I had to add context switching with 'USE master'.

George.

|||

Hi... you mentioned you had the below source sample...

The VB code is a little complicated with using app.config to open so many network drives and folders, but if anybody is interested I'll post it.

Can you please post or sent to billbbellevue@.hotmail.com

Kind regards,

Bill Bezick

Saturday, February 11, 2012

#Temp table question

I'm not sure if I understood this correctly.
If I create a temporary table with the name #SomeTempTab,
can anybody else see this table while I'm using it and before I drop it.
So if two users at the same time trying to create the table with this name
will they be different for each user or there will be a conflict?
Thanks,
Michael
On Fri, 31 Dec 2004 16:12:06 -0800, MichaelK wrote:

>I'm not sure if I understood this correctly.
>If I create a temporary table with the name #SomeTempTab,
>can anybody else see this table while I'm using it and before I drop it.
>So if two users at the same time trying to create the table with this name
>will they be different for each user or there will be a conflict?
Hi Michael,
There will be no conflict. SQL Server will generate a connection-specific
suffix to create a really unique table name in tempdb. Everytime you refer
to #SomeTempTab, SQL Server will append the suffix and look in "your"
version of the temporary table.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Thanks Hugo.
You were helpfull as well as with my another question.
It's what I was looking for.
Regards,
Michael
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:ej7et0980uvbk8n3nheabcvn0lfq57as4a@.4ax.com...
> On Fri, 31 Dec 2004 16:12:06 -0800, MichaelK wrote:
>
> Hi Michael,
> There will be no conflict. SQL Server will generate a connection-specific
> suffix to create a really unique table name in tempdb. Everytime you refer
> to #SomeTempTab, SQL Server will append the suffix and look in "your"
> version of the temporary table.
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)

Thursday, February 9, 2012

##Temp tables

I have a stored proc that creates a temporary table, then calls several other stored procs to insert data.

CREATE PROCEDURE usp_CreateTakeoff
@.iEstimate int,
AS

CREATE TABLE ##Temp_Takeoff
(
Field1 .....
Field2 .....
)

-- Add Structural data
usp_AddStructural @.iEstimateID, 1, 'Structural'
usp_AddForming @.iEstimateID, 2, 'Forming'
...
...
...
GO

Now, a couple of problems, after the table is created and populated, I cannot find it in my list of tables, even after "refreshing".

I checked to ensure that it exists using the query analyzer and it does so I know the table is being created.

Also, I cannot see the table using crystal reports, connecting etc..... Can I not access a temporary table from 3rd party applications? I have crystal reports 7.0 professional.

Any ideas?

Mike BGlobal and local temp tables are created in tempdb.|||Global and local temp tables are created in tempdb.
Now, if I remember correctly, 1 (#) indicates global and 2 (#) indicates local.

So if multiple users executed the stored proc with ##Temp, then each connection would create a table unique to the connection? So multiple users could execute this proc without interfering with each other?

Mike B|||Your memory fails you young padowan. ## is a global temp table which will cease to exist when the last connection to it ceases to exist. The # temp table is a local table for the duration of the process.|||Your memory fails you young padowan. ## is a global temp table which will cease to exist when the last connection to it ceases to exist. The # temp table is a local table for the duration of the process.
Not the first time, won't be the last I am afraid! :) Thanks for the correction.

Mike