Showing posts with label procedure. Show all posts
Showing posts with label procedure. Show all posts

Friday, March 30, 2012

OpenQuery With Large String?

Hi,

I declare a variable @.MdxSyntax as NVARCHAR(4000) to store MDX OpenQuery syntax on Store Procedure.

SET @.mdxSyntax =
'
SELECT * INTO ##BU01505100
FROM OPENQUERY
(MOJOLAP,
''
WITH

'')
'

EXEC sp_executesql @.mdxSyntax

But maybe the syntax too long, system response syntax unclosed!

So, I change @.MdxSyntax as NVARCHAR(MAX), but it still response syntax unclosed.

Why? It's the limit of OpenQuery or MDX?

Thanks for help!

Note:

OPENQUERY does not accept variables for its arguments.

You have to use the query as String values on OPENQUERY.

|||

ManiD,

Thanks for your reply!

But my point is no matter what I declare @.mdxSyntax as NVARCHAR(4000) or NVARCHAR(MAX),

the query result always response syntax unclosed. WHY?

OPENQUERY vs. sp_executesql which is better?

I have recently seen the OPENQUERY function in a stored procedure which
was used to INSERT values into a table on a remote server. However, I
normally use the sp_executesql stored proc.
Can any one shed some light on which method is better and in what
cases?
The following code is from Microsoft website, not my own.
http://msdn.microsoft.com/library/d...br />
5xix.asp
<MS CODE>
EXEC sp_addlinkedserver 'OracleSvr',
'Oracle 7.3',
'MSDAORA',
'ORCLDB'
GO
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
GO
</MS CODE>
VS.
<MYCODE>
sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
-- OR
OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
</MYCODE>
Thanks for your time folks.
Johnny DWhy would you even consider sp_executesql in your case? There is no need to
use dynamic sql to access a linked server.
Andrew J. Kelly SQL MVP
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1148649898.280530.174390@.j73g2000cwa.googlegroups.com...
>I have recently seen the OPENQUERY function in a stored procedure which
> was used to INSERT values into a table on a remote server. However, I
> normally use the sp_executesql stored proc.
> Can any one shed some light on which method is better and in what
> cases?
> The following code is from Microsoft website, not my own.
> http://msdn.microsoft.com/library/d... />
z_5xix.asp
> <MS CODE>
> EXEC sp_addlinkedserver 'OracleSvr',
> 'Oracle 7.3',
> 'MSDAORA',
> 'ORCLDB'
> GO
>
> SELECT *
> FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
> GO
> </MS CODE>
> VS.
> <MYCODE>
> sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
> -- OR
> OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
> </MYCODE>
>
> Thanks for your time folks.
>
> Johnny D
>|||Sorry Andrew, this was an oversight in me writing a simple query to
illustrate my question...
The reason for this is I would use a cursor to loop through my
different servernames
DECLARE @.rc INT
DECLARE @.v_sql NVARCHAR(4000)
DECLARE @.vc_servername VARCHAR(100)
DECLARE c_myservers CURSOR
FOR SELECT
servername
FROM listservers
WHERE active=1
FOR READ ONLY
OPEN c_myservers
FETCH NEXT FROM c_myservers INTO @.vc_servername
WHILE (@.@.FETCHSTATUS = 0 )
BEGIN
SET @.v_sql = N'SELECT name, id FROM ['+@.vc_servername +
'].dbname.joe.titles'
EXEC @.rc = sp_executesql @.v_sql
-- if @.rc <> 0
-- etc...
FETCH NEXT FROM c_myservers INTO @.vc_servername
END
CLOSE c_myservers
DEALLOCATE c_myservers
sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'|||Well one key point of using OPENQUERY is that it passes the statement to the
other server where it is executed as is. That way the remote server can
choose the proper plan for that statement without regard to what the rest of
the statement is that was issued locally. Kind of hard to explain but it is
truly a pass-through query where as a linked server query may be influenced
by the rest of the statement. For instance a join to a local table.
Andrew J. Kelly SQL MVP
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1148652836.663893.44660@.g10g2000cwb.googlegroups.com...
> Sorry Andrew, this was an oversight in me writing a simple query to
> illustrate my question...
> The reason for this is I would use a cursor to loop through my
> different servernames
> DECLARE @.rc INT
> DECLARE @.v_sql NVARCHAR(4000)
> DECLARE @.vc_servername VARCHAR(100)
> DECLARE c_myservers CURSOR
> FOR SELECT
> servername
> FROM listservers
> WHERE active=1
> FOR READ ONLY
> OPEN c_myservers
> FETCH NEXT FROM c_myservers INTO @.vc_servername
> WHILE (@.@.FETCHSTATUS = 0 )
> BEGIN
> SET @.v_sql = N'SELECT name, id FROM ['+@.vc_servername +
> '].dbname.joe.titles'
> EXEC @.rc = sp_executesql @.v_sql
> -- if @.rc <> 0
> -- etc...
> FETCH NEXT FROM c_myservers INTO @.vc_servername
> END
> CLOSE c_myservers
> DEALLOCATE c_myservers
>
> sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
>|||Johnny D (john.dacosta@.gmail.com) writes:
> I have recently seen the OPENQUERY function in a stored procedure which
> was used to INSERT values into a table on a remote server. However, I
> normally use the sp_executesql stored proc.
> Can any one shed some light on which method is better and in what
> cases?
I think they are as comparable as apples and oranges.
Apparently you loop over servers. That is nothing OPENQUERY can help
you with - the server name must be a constant.
Rather your choice is between:
SELECT @.sql = 'SELECT ... FROM ' + @.server + 'catalog.schema.tbl'
and
SELECT @.sql = 'SELECT ... FROM OPENQUERY(' + @.server + ', ' +
'''SELECT ... FROM catalog.schema.tbl'')'
That is, accessing the table in four-partnotation, or running a
passthrough query.

><MYCODE>
> OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
></MYCODE>
Ehum, I don't think you will find sp_executesql on Oracle...
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

OPENQUERY question

I'd like to use a linked server to a Setcim database to retrieve results fro
m
a procedure in the Setcim database.
The syntax used to call the view in Setcim is:
start record 'BSW1View'
which works when using the SQLplus tools for Setcim.
I was assuming I could OPENQUERY to call this record and then put the data
into a SQL Server database but I get syntax errors when I try various
combinations of the following:
SELECT *
FROM OPENQUERY(PRISM,'start record 'BSW1View'')
The issue appears to be the single quotes around the procedure name.
Can I do what I want using OPENQUERY? If not, what options are available?
Thanks in advance,
RaulDouble them (the inner apostrophes).
SELECT *
FROM OPENQUERY(PRISM,'start record ''BSW1View''')
AMB
"Raul" wrote:

> I'd like to use a linked server to a Setcim database to retrieve results f
rom
> a procedure in the Setcim database.
> The syntax used to call the view in Setcim is:
> start record 'BSW1View'
> which works when using the SQLplus tools for Setcim.
> I was assuming I could OPENQUERY to call this record and then put the data
> into a SQL Server database but I get syntax errors when I try various
> combinations of the following:
> SELECT *
> FROM OPENQUERY(PRISM,'start record 'BSW1View'')
> The issue appears to be the single quotes around the procedure name.
> Can I do what I want using OPENQUERY? If not, what options are available?
> Thanks in advance,
> Raul
>|||Your suggestion worked. The only problem is I got the following error:
Server: Msg 7357, Level 16, State 2, Line 1
Could not process object 'start record 'BSW1View''. The OLE DB provider
'MSDASQL' indicates that the object has no columns.
I'll modify the procedure on the Setcim side and try again.
Thanks for the help,
Raul
"Alejandro Mesa" wrote:
> Double them (the inner apostrophes).
> SELECT *
> FROM OPENQUERY(PRISM,'start record ''BSW1View''')
>
> AMB
> "Raul" wrote:
>

Openquery Help Requested

sql server = server A
sybase = server B (set up as a linked server in A)
I have a stored procedure in A that contains the following statement:
SELECT * FROM OPENQUERY(B, 'exec storedproc')
which runs the procedure in B just fine. It truncates a table in B and
then populates it with data from various queries. The issue is later in
the stored procedure issued from A I perform an insert where I grab the
data inserted into the table in B and insert that data into a table in
A. It seems as though everytime a number of records are inserted into
the table in A the statement above: SELECT * FROM OPENQUERY(B, 'exec
storedproc') is run again and again. Does anybody know why this is
occurring and how I can stop this from occurring?
Thanks,
E> the stored procedure issued from A I perform an insert where I grab the
> data inserted into the table in B and insert that data into a table in
> A. It seems as though everytime a number of records are inserted into
> the table in A the statement above: SELECT * FROM OPENQUERY(B, 'exec
> storedproc') is run again and again. Does anybody know why this is
> occurring and how I can stop this from occurring?
You mean that later within 'storedproc' you are perfoming insert into a
table that is resided on server 'A' and you have seen in SQL Server Profiler
that it runs SELECT * FROM OPENQUERY(B, 'exec> storedproc') many times. Am
I right?
"w" <ebrooks@.sbcglobal.net> wrote in message
news:1136953074.780909.282800@.g14g2000cwa.googlegroups.com...
> sql server = server A
> sybase = server B (set up as a linked server in A)
> I have a stored procedure in A that contains the following statement:
> SELECT * FROM OPENQUERY(B, 'exec storedproc')
> which runs the procedure in B just fine. It truncates a table in B and
> then populates it with data from various queries. The issue is later in
> the stored procedure issued from A I perform an insert where I grab the
> data inserted into the table in B and insert that data into a table in
> A. It seems as though everytime a number of records are inserted into
> the table in A the statement above: SELECT * FROM OPENQUERY(B, 'exec
> storedproc') is run again and again. Does anybody know why this is
> occurring and how I can stop this from occurring?
> Thanks,
> E
>|||Well the reason I know that it keeps reissuing that statement is that
while the table in A is being populated after a number of records have
been inserted the table in B is truncated and repopulated again and
this cycle goes on until the table in A has been completely populated.
E
Uri Dimant wrote:
> You mean that later within 'storedproc' you are perfoming insert into a
> table that is resided on server 'A' and you have seen in SQL Server Profil
er
> that it runs SELECT * FROM OPENQUERY(B, 'exec> storedproc') many times. A
m
> I right?
>
> "w" <ebrooks@.sbcglobal.net> wrote in message
> news:1136953074.780909.282800@.g14g2000cwa.googlegroups.com...|||Well the reason I know that it keeps reissuing that statement is that
while the table in A is being populated after a number of records have
been inserted the table in B is truncated and repopulated again and
this cycle goes on until the table in A has been completely populated.
E
Uri Dimant wrote:
> You mean that later within 'storedproc' you are perfoming insert into a
> table that is resided on server 'A' and you have seen in SQL Server Profil
er
> that it runs SELECT * FROM OPENQUERY(B, 'exec> storedproc') many times. A
m
> I right?
>
> "w" <ebrooks@.sbcglobal.net> wrote in message
> news:1136953074.780909.282800@.g14g2000cwa.googlegroups.com...

Monday, March 26, 2012

Opening password protected Excel files

I have a stored procedure in which I am importing data from a password protected Excel file to a SQL table. The file comes from our client and the password cannot be removed before sending, per his company policy.

I would like to create a DTS package to open the file and import the data. Is there a way to pass the password to the DTS package?

Thanks!May refer to this KBA (http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q211378).|||I can do this if I am using Excel, but I need to do it from a stored procedure. Is this possible using TSQL?

Thanks!|||Honestly I never tried, you may search under SQL DTS (http://www.sqldts.com) for further information.|||I got it!!! One of my coworkers (who was out when I needed the answer yesterday) had the solution!

dtsrun /Sservername /Uuid /Ppwd /Npackagename /AFileName:8= excelfilename /APassword:8=excelpwd

:-)|||Good keep it up, your reply help others.

Opening access via ip

Hi
sbs 2003 with isa 2000. I need to access an external (to the sbs/isa domain)
server using its ip address. What is the procedure to set-up ISA to allow
this?
Thanks
Regards
Hi
"John" wrote:

> Hi
> sbs 2003 with isa 2000. I need to access an external (to the sbs/isa domain)
> server using its ip address. What is the procedure to set-up ISA to allow
> this?
> Thanks
> Regards
>
You have cross posted this into may groups some of which are not ISA
related. Please only post to active relevant groups.
John

Opening access via ip

Hi
sbs 2003 with isa 2000. I need to access an external (to the sbs/isa domain)
server using its ip address. What is the procedure to set-up ISA to allow
this?
Thanks
Regards
Hi
"John" wrote:

> Hi
> sbs 2003 with isa 2000. I need to access an external (to the sbs/isa domain)
> server using its ip address. What is the procedure to set-up ISA to allow
> this?
> Thanks
> Regards
>
You have cross posted this into may groups some of which are not ISA
related. Please only post to active relevant groups.
John

Opening access via ip

Hi
sbs 2003 with ISA 2000. I need to access an external (to the sbs/isa domain)
server using its ip address. What is the procedure to set-up ISA to allow
this?
Thanks
RegardsHi
"John" wrote:

> Hi
> sbs 2003 with ISA 2000. I need to access an external (to the sbs/isa domai
n)
> server using its ip address. What is the procedure to set-up ISA to allow
> this?
> Thanks
> Regards
>
You have cross posted this into may groups some of which are not ISA
related. Please only post to active relevant groups.
Johnsql

Opening a SP to modify gives incorrect name on TAB

In SQL Server Mangement Studio when I right click on stored procedure to modify the name on the tab does not have the name of the stored procedure on it.

Is there a way to turn this on? When I more than one open how do I tell where a certain sp is?

what u see in Management Studio is Schema.StoredProcedureName... u might be seeing in Summary window where u have Name first , then Schema and Then Created... Both are same ...

Madhu

|||

The tab name currently displays an autogenerated file name with server and database information (same as what a new query would do). It does not show the stored procedure name. Currently there is no way to turn this on. Please post a suggestion for this on http://connect.microsoft.com/sqlserver. Suggestions filed on this site go directly to our internal issue tracking system and are especially taken into consideration by the team while prioritizing the future work.

Thanks,
Kuntal

Opening a microsoft application from a stored procedure

Is there a way to call/launch a word document from a stored procedure?
Thanks for any help.Technically, yes. But why would you want to do such a crazy thing?
If you decided that you'd rather continue drinking the happy juice, you coul
d
use the sp_OA functions to call out to a COM dll that opens the Word documen
t
and does who knows what with it. If you were really ambitious, you could wri
te
an extended stored procedure in C++ to do the same thing.
Thomas
"Munch" <Munch@.discussions.microsoft.com> wrote in message
news:94F4A9C2-5399-4F64-B2D0-6C654A2DCCF7@.microsoft.com...
> Is there a way to call/launch a word document from a stored procedure?
> Thanks for any help.|||Would it be better to insert the path of the documents /files into a table
and have them executed from the tables? Is this possible? I know we used t
o
house jpgs in a table and referenced different jpgs depending on the user an
d
preferences. Can we do this with word docs and other MS Applications?
Thanks
"Thomas" wrote:

> Technically, yes. But why would you want to do such a crazy thing?
> If you decided that you'd rather continue drinking the happy juice, you co
uld
> use the sp_OA functions to call out to a COM dll that opens the Word docum
ent
> and does who knows what with it. If you were really ambitious, you could w
rite
> an extended stored procedure in C++ to do the same thing.
>
> Thomas
>
> "Munch" <Munch@.discussions.microsoft.com> wrote in message
> news:94F4A9C2-5399-4F64-B2D0-6C654A2DCCF7@.microsoft.com...
>
>|||Yes you can store any type of document in the database by using image column
s. A
column of type image can store any type of binary in its raw format. So, jus
t as
JPEGs can be stored in the db, so can Word documents. However, *storing* Wor
d
documents in the database is a far cry from *launching* Word from SQL Server
.
Determining whether you should store the actual files in the database instea
d of
a path is a cost-benefit analysis. Storing the files in the database makes
things like replication a bit easier and provides some security advantages.
In
addition, it makes accessing the file a little simpler in that everything is
retrieved from the database. The downside is that it makes it difficult to e
dit
the files as you need to make a program to pull and push the file to the
database. In addition, it bloats the size of the database making database
backups take longer.
Storing the path to the files makes it possible to view and edit the files
directly. However, you have to ensure that the path in the database stays in
sync with the path and filename on the disk. Where the path solution gets re
ally
tricky is with multiple servers and multiple sites. If that is not a concern
,
the I'd go with the path solution. I would ensure that websites cannot acces
s
the files directly, but rather go through a proxy that sends the binary to t
he
website code. In this way, you shield the storage mechanism and location fro
m
the website adding security. In addition, it allows you to easily switch
solutions without having to change the website code.
HTH
Thomas
"Munch" <Munch@.discussions.microsoft.com> wrote in message
news:28ACA9AE-D640-449C-9257-6690BA761FAA@.microsoft.com...
> Would it be better to insert the path of the documents /files into a table
> and have them executed from the tables? Is this possible? I know we used
to
> house jpgs in a table and referenced different jpgs depending on the user
and
> preferences. Can we do this with word docs and other MS Applications?
> Thanks
> "Thomas" wrote:
>|||Thomas:
Thanks for the valuable info. I will look into these options.
"Thomas Coleman" wrote:

> Yes you can store any type of document in the database by using image colu
mns. A
> column of type image can store any type of binary in its raw format. So, j
ust as
> JPEGs can be stored in the db, so can Word documents. However, *storing* W
ord
> documents in the database is a far cry from *launching* Word from SQL Serv
er.
> Determining whether you should store the actual files in the database inst
ead of
> a path is a cost-benefit analysis. Storing the files in the database makes
> things like replication a bit easier and provides some security advantages
. In
> addition, it makes accessing the file a little simpler in that everything
is
> retrieved from the database. The downside is that it makes it difficult to
edit
> the files as you need to make a program to pull and push the file to the
> database. In addition, it bloats the size of the database making database
> backups take longer.
> Storing the path to the files makes it possible to view and edit the files
> directly. However, you have to ensure that the path in the database stays
in
> sync with the path and filename on the disk. Where the path solution gets
really
> tricky is with multiple servers and multiple sites. If that is not a conce
rn,
> the I'd go with the path solution. I would ensure that websites cannot acc
ess
> the files directly, but rather go through a proxy that sends the binary to
the
> website code. In this way, you shield the storage mechanism and location f
rom
> the website adding security. In addition, it allows you to easily switch
> solutions without having to change the website code.
>
> HTH
>
> Thomas
>
> "Munch" <Munch@.discussions.microsoft.com> wrote in message
> news:28ACA9AE-D640-449C-9257-6690BA761FAA@.microsoft.com...
>
>

Friday, March 23, 2012

OpenDataSource with Microsoft.Jet.OLEDB.4 Excel from stored proced

I have a stored procedure which has the following line:
select * from FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="\\MachineName\shared\account.xls";...Excel 8.0...
This stored procedure was called from a VB application. It's odd that
ONLY when ADO connection user need SA database role
AND \\MachineName\shared\account.xls was physical located in the same
machine of DATABASE
the stored procedure would work.
Otherwise it returned error like:
OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0'
IDBInitialize::Initialize returned 0x80004005: The provider did not give an
y
information about the error.]. 0 7300 -2147217900 Microsoft OLE DB Provider
for SQL Server 01000OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an
error. The provider did not give any information about the error. 0 7399
-2147217900 Microsoft OLE DB Provider for SQL Server 42000
Database is SQL Server 2000 SP4. Registry DisallowAdhocAccess is 0.
Any suggestion?Because the security reson, I don't want ADO connection user has the SA role
,
and Excel file can located in any shared folder in the LAN.
"david" wrote:

> I have a stored procedure which has the following line:
> select * from FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
> 'Data Source="\\MachineName\shared\account.xls";...Excel 8.0...
> This stored procedure was called from a VB application. It's odd that
> ONLY when ADO connection user need SA database role
> AND \\MachineName\shared\account.xls was physical located in the same
> machine of DATABASE
> the stored procedure would work.
> Otherwise it returned error like:
> OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0'
> IDBInitialize::Initialize returned 0x80004005: The provider did not give
any
> information about the error.]. 0 7300 -2147217900 Microsoft OLE DB Provide
r
> for SQL Server 01000OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an
> error. The provider did not give any information about the error. 0 7399
> -2147217900 Microsoft OLE DB Provider for SQL Server 42000
> Database is SQL Server 2000 SP4. Registry DisallowAdhocAccess is 0.
> Any suggestion?
>sql

OpenDataSource with Microsoft.Jet.OLEDB.4 Excel from stored proced

I have a stored procedure which has the following line:
select * from FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="\\MachineName\shared\account.xls";...Excel 8.0...
This stored procedure was called from a VB application. It's odd that
ONLY when ADO connection user need SA database role
AND \\MachineName\shared\account.xls was physical located in the same
machine of DATABASE
the stored procedure would work.
Otherwise it returned error like:
OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0'
IDBInitialize::Initialize returned 0x80004005: The provider did not give any
information about the error.]. 0 7300 -2147217900 Microsoft OLE DB Provider
for SQL Server 01000OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an
error. The provider did not give any information about the error. 0 7399
-2147217900 Microsoft OLE DB Provider for SQL Server 42000
Database is SQL Server 2000 SP4. Registry DisallowAdhocAccess is 0.
Any suggestion?Because the security reson, I don't want ADO connection user has the SA role,
and Excel file can located in any shared folder in the LAN.
"david" wrote:
> I have a stored procedure which has the following line:
> select * from FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
> 'Data Source="\\MachineName\shared\account.xls";...Excel 8.0...
> This stored procedure was called from a VB application. It's odd that
> ONLY when ADO connection user need SA database role
> AND \\MachineName\shared\account.xls was physical located in the same
> machine of DATABASE
> the stored procedure would work.
> Otherwise it returned error like:
> OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0'
> IDBInitialize::Initialize returned 0x80004005: The provider did not give any
> information about the error.]. 0 7300 -2147217900 Microsoft OLE DB Provider
> for SQL Server 01000OLE DB provider 'Microsoft.Jet.OLEDB.4.0' reported an
> error. The provider did not give any information about the error. 0 7399
> -2147217900 Microsoft OLE DB Provider for SQL Server 42000
> Database is SQL Server 2000 SP4. Registry DisallowAdhocAccess is 0.
> Any suggestion?
>

Wednesday, March 21, 2012

Open stored in Hexadecimal

How I can to open a encrypted stored procedure in Hexadecimal mode
with Query Analyzer?

ThanksYachi <yachi@.pointel.it> wrote in message news:<bpujh0himbt42fv9obbj477tnnqeu9162b@.4ax.com>...
> How I can to open a encrypted stored procedure in Hexadecimal mode
> with Query Analyzer?
> Thanks

In general, you won't get an answer to questions like this in a
newsgroup, because it's not always clear what the motive behind the
question is. However, if you really need to know, then Google should
be able to help...

Simon

Monday, March 12, 2012

Open encryption key in stored procedure

Hi,
I would like to create a stored procedure, which accept RunAsUser, MasterKeyPassword and also ASymmetricKeyPassword. In this stored procedure, it call OPEN MASTER KEY and etc. I put these code in stored procedure so that my support staff can call it when doing maintenance by passing the parameter. I don't expect them to remember OPEN MASTER KEY and etc syntax.

However, I hit error "Invalid Syntax" when I run my code as below. Any ideas?

Thank you

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE GrantMe
-- Add the parameters for the stored procedure here
@.RunAsUser varchar(20), @.MasterKey varchar(30), @.ASKey varchar(30)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

EXEC AS USER=@.RunAsUser;

OPEN MASTER KEY DECRYPTION BY PASSWORD=@.MasterKey;

OPEN SYMMETRIC KEY HRMS
DECRYPTION BY ASYMMETRIC KEY FlexHRMS WITH PASSWORD=@.AsKey ;
END
GO

I don't believe DDL supports parameterization. You could use dynamic SQL, but it should be easier to simply use the DecryptByKeyAutoAsymKey built-in which will automatically open the symmetric key for you so you don't have to use the OPEN command.

Sung

|||

BTW, you should not have to call OPEN MASTER KEY if the ASYMMETRIC KEY is encrypted by password.

Sung

|||

DDL statements don’t support variables as arguments, therefore you need to change the OPEN MASTER KEY and OPEN SYMMETRIC KEY statements. Unfortunately , you will need to use dynamic SQL (the non-parameterized form) in order to execute such statements with user-defined content. Because you will need to create non-parameterized dynamic SQL, I really recommend reading about SQL injection and how to prevent it; below I included a few links regarding SQL Injection:

· Dynamic SQL & SQL Injection http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx

· SQL Injection http://msdn2.microsoft.com/en-us/library/ms161953.aspx

· New SQL Truncation Attacks And How To Avoid Them http://msdn.microsoft.com/msdnmag/issues/06/11/SQLSecurity/default.aspx

· Stop SQL Injection Attacks Before They Stop You http://msdn.microsoft.com/msdnmag/issues/04/09/sqlinjection/default.aspx

· Second-order Code Injection Attacks (http://www.ngssoftware.com/papers/SecondOrderCodeInjection.pdf)

I have some additional comments that hopefully will also help.

I am assuming that the intention of this SP is to open the DB master key (meaning it is not protected by the service master key) and open the symmetric key HRMS, leaving both keys opened in order to allow both of them to be used later in the same session, correct?

I would recommend adding comments specifying the intention of opening the keys and not closing them is intentional.

I am also assuming that the impersonation (EXECUTE AS USER) takes place because the calling context doesn’t have direct permission over either the symmetric key or the asymmetric key protecting it. Because the caller would need IMPERSONATE USER permission on @.RunAsUser, and this permission allows the calling user to impersonate that principal anytime, you may want to consider using EXECUTE AS in the module definition, or use digital signatures for allowing access to the keys instead.

I hope this information will be useful.

-Raul Garcia

SDE/T

SQL Server Engine

|||Hi,
Thank you for you comments.

>>digital signatures for allowing access to the keys instead.

Any reference for this?

Thank you

|||

Yes, see the following link:

http://msdn2.microsoft.com/en-us/library/ms181700.aspx

For examples, I have a few on my blog:

http://blogs.msdn.com/lcris/archive/2005/06/15/429631.aspx
http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx

Thanks
Laurentiu

Open Encrypted Stored Procedure

Hi, i has another problem i think you can help me on,
I need to open a encrypted stored procedure, how can i do it??
Thanks.Ohhhh.. Microsoft and his security realy sucks...
here you have how.

Create Procedure Decryptsp2K (@.objName varchar(50))
AS
-- INPUT: object name (stored procedure, view or trigger)
DECLARE @.a nvarchar(4000), @.b nvarchar(4000), @.c nvarchar(4000), @.d nvarchar(4000), @.i int, @.t bigint
--get encrypted data
SET @.a=(SELECT ctext FROM syscomments WHERE id = object_id(@.objName))
SET @.b='ALTER PROCEDURE '+ @.objName +' WITH ENCRYPTION AS '+REPLICATE('-', 4000-62)
EXECUTE (@.b)
--get encrypted bogus SP
SET @.c=(SELECT ctext FROM syscomments WHERE id = object_id(@.objName))
SET @.b='CREATE PROCEDURE '+ @.objName +' WITH ENCRYPTION AS '+REPLICATE('-', 4000-62)
--start counter
SET @.i=1
--fill temporary variable
SET @.d = replicate(N'A', (datalength(@.a) / 2))
--loop
WHILE @.i<=datalength(@.a)/2
BEGIN
--xor original+bogus+bogus encrypted
SET @.d = stuff(@.d, @.i, 1,
NCHAR(UNICODE(substring(@.a, @.i, 1)) ^
(UNICODE(substring(@.b, @.i, 1)) ^
UNICODE(substring(@.c, @.i, 1)))))
SET @.i=@.i+1
END
--drop original SP
EXECUTE ('drop PROCEDURE '+ @.objName)
--remove encryption
--try to preserve case
SET @.d=REPLACE((@.d),'WITH ENCRYPTION', '')
SET @.d=REPLACE((@.d),'With Encryption', '')
SET @.d=REPLACE((@.d),'with encryption', '')
IF CHARINDEX('WITH ENCRYPTION',UPPER(@.d) )>0
SET @.d=REPLACE(UPPER(@.d),'WITH ENCRYPTION', '')
--replace SP
execute( @.d)

GO|||Thank you for you code, is exactly what i need and works perfect!

Open Connections - Can't Drop

Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
I have no problems Dropping the Tables.
Thanks,
Chuck
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>
|||Hello Thank You for you help.
I have the Stored Procedure inside the Master Table.
I am immediately running the query given after connecting. I also cannot
remove it from SQL Express Management Studio. - That is if I copy the SQL
into a query window and run it. I get the exact same error.
Chuck
"Hardik Bati" <hardikb_removethis_@.microsoft.com> wrote in message
news:OLkzPow9GHA.4376@.TK2MSFTNGP03.phx.gbl...
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>
|||Hello,
I have been doing some testing. I am filling a Dataset using a DataAdapter.
If I create a new connection to SQL Express without calling the
DataAdapter's Fill Method I can Drop the Database.
If I Fill the Dataset with the DataAdapter, dispose the connection and the
DataAdapter and create a new connection it still wont Drop the Database.
Even if the Dataset is empty it still does not work. It appears that
calling the DataAdapter before dropping the table keeps the database in use
weather it has finished it's job or not.
I can't even call a Stored Procedure to drop the Database if I call the
DataAdapter First. Even if I run the program and call the DataAdapter and
go into SQL Express Management Studio, I still get the same error. Only
when I shut down the program or drop the table without using the DataAdapter
does it work.
Is there a way around this?
Thanks
Chuck
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
I have no problems Dropping the Tables.
Thanks,
Chuck
|||The most likely cause is that the closed/disposed connection is pooled and
the pooled connection is preventing you from dropping the database. You can
specify Pooling=False in the connection string (or similar, depending on the
provider you are using) to disable connection pooling.
Another method is to execute "ALTER DATABASE MyDatabase SET SINGLE_USER WITH
ROLLBACK IMMEDIATE" to kill all connections to the database befor the drop.
Hope this helps.
Dan Guzman
SQL Server MVP
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:OgZmXqz9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I have been doing some testing. I am filling a Dataset using a
> DataAdapter.
> If I create a new connection to SQL Express without calling the
> DataAdapter's Fill Method I can Drop the Database.
> If I Fill the Dataset with the DataAdapter, dispose the connection and the
> DataAdapter and create a new connection it still wont Drop the Database.
> Even if the Dataset is empty it still does not work. It appears that
> calling the DataAdapter before dropping the table keeps the database in
> use
> weather it has finished it's job or not.
> I can't even call a Stored Procedure to drop the Database if I call the
> DataAdapter First. Even if I run the program and call the DataAdapter and
> go into SQL Express Management Studio, I still get the same error. Only
> when I shut down the program or drop the table without using the
> DataAdapter
> does it work.
> Is there a way around this?
> Thanks
> Chuck
>
> "Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
> news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>

Open Connections - Can't Drop

Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
I have no problems Dropping the Tables.
Thanks,
ChuckWhere is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>|||Hello Thank You for you help.
I have the Stored Procedure inside the Master Table.
I am immediately running the query given after connecting. I also cannot
remove it from SQL Express Management Studio. - That is if I copy the SQL
into a query window and run it. I get the exact same error.
Chuck
"Hardik Bati" <hardikb_removethis_@.microsoft.com> wrote in message
news:OLkzPow9GHA.4376@.TK2MSFTNGP03.phx.gbl...
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>|||Hello,
I have been doing some testing. I am filling a Dataset using a DataAdapter.
If I create a new connection to SQL Express without calling the
DataAdapter's Fill Method I can Drop the Database.
If I Fill the Dataset with the DataAdapter, dispose the connection and the
DataAdapter and create a new connection it still wont Drop the Database.
Even if the Dataset is empty it still does not work. It appears that
calling the DataAdapter before dropping the table keeps the database in use
weather it has finished it's job or not.
I can't even call a Stored Procedure to drop the Database if I call the
DataAdapter First. Even if I run the program and call the DataAdapter and
go into SQL Express Management Studio, I still get the same error. Only
when I shut down the program or drop the table without using the DataAdapter
does it work.
Is there a way around this?
Thanks
Chuck
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
I have no problems Dropping the Tables.
Thanks,
Chuck|||The most likely cause is that the closed/disposed connection is pooled and
the pooled connection is preventing you from dropping the database. You can
specify Pooling=False in the connection string (or similar, depending on the
provider you are using) to disable connection pooling.
Another method is to execute "ALTER DATABASE MyDatabase SET SINGLE_USER WITH
ROLLBACK IMMEDIATE" to kill all connections to the database befor the drop.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:OgZmXqz9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I have been doing some testing. I am filling a Dataset using a
> DataAdapter.
> If I create a new connection to SQL Express without calling the
> DataAdapter's Fill Method I can Drop the Database.
> If I Fill the Dataset with the DataAdapter, dispose the connection and the
> DataAdapter and create a new connection it still wont Drop the Database.
> Even if the Dataset is empty it still does not work. It appears that
> calling the DataAdapter before dropping the table keeps the database in
> use
> weather it has finished it's job or not.
> I can't even call a Stored Procedure to drop the Database if I call the
> DataAdapter First. Even if I run the program and call the DataAdapter and
> go into SQL Express Management Studio, I still get the same error. Only
> when I shut down the program or drop the table without using the
> DataAdapter
> does it work.
> Is there a way around this?
> Thanks
> Chuck
>
> "Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
> news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>

Open Connections - Can't Drop

Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
I have no problems Dropping the Tables.
Thanks,
Chuck
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>
|||Hello Thank You for you help.
I have the Stored Procedure inside the Master Table.
I am immediately running the query given after connecting. I also cannot
remove it from SQL Express Management Studio. - That is if I copy the SQL
into a query window and run it. I get the exact same error.
Chuck
"Hardik Bati" <hardikb_removethis_@.microsoft.com> wrote in message
news:OLkzPow9GHA.4376@.TK2MSFTNGP03.phx.gbl...
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>
|||Hello,
I have been doing some testing. I am filling a Dataset using a DataAdapter.
If I create a new connection to SQL Express without calling the
DataAdapter's Fill Method I can Drop the Database.
If I Fill the Dataset with the DataAdapter, dispose the connection and the
DataAdapter and create a new connection it still wont Drop the Database.
Even if the Dataset is empty it still does not work. It appears that
calling the DataAdapter before dropping the table keeps the database in use
weather it has finished it's job or not.
I can't even call a Stored Procedure to drop the Database if I call the
DataAdapter First. Even if I run the program and call the DataAdapter and
go into SQL Express Management Studio, I still get the same error. Only
when I shut down the program or drop the table without using the DataAdapter
does it work.
Is there a way around this?
Thanks
Chuck
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
I have no problems Dropping the Tables.
Thanks,
Chuck
|||The most likely cause is that the closed/disposed connection is pooled and
the pooled connection is preventing you from dropping the database. You can
specify Pooling=False in the connection string (or similar, depending on the
provider you are using) to disable connection pooling.
Another method is to execute "ALTER DATABASE MyDatabase SET SINGLE_USER WITH
ROLLBACK IMMEDIATE" to kill all connections to the database befor the drop.
Hope this helps.
Dan Guzman
SQL Server MVP
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:OgZmXqz9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I have been doing some testing. I am filling a Dataset using a
> DataAdapter.
> If I create a new connection to SQL Express without calling the
> DataAdapter's Fill Method I can Drop the Database.
> If I Fill the Dataset with the DataAdapter, dispose the connection and the
> DataAdapter and create a new connection it still wont Drop the Database.
> Even if the Dataset is empty it still does not work. It appears that
> calling the DataAdapter before dropping the table keeps the database in
> use
> weather it has finished it's job or not.
> I can't even call a Stored Procedure to drop the Database if I call the
> DataAdapter First. Even if I run the program and call the DataAdapter and
> go into SQL Express Management Studio, I still get the same error. Only
> when I shut down the program or drop the table without using the
> DataAdapter
> does it work.
> Is there a way around this?
> Thanks
> Chuck
>
> "Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
> news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuer y()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>

Open Connections - Can't Drop

Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
I have no problems Dropping the Tables.
Thanks,
ChuckWhere is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>|||Hello Thank You for you help.
I have the Stored Procedure inside the Master Table.
I am immediately running the query given after connecting. I also cannot
remove it from SQL Express Management Studio. - That is if I copy the SQL
into a query window and run it. I get the exact same error.
Chuck
"Hardik Bati" <hardikb_removethis_@.microsoft.com> wrote in message
news:OLkzPow9GHA.4376@.TK2MSFTNGP03.phx.gbl...
Where is this stored procedure. If it's in the database then you won't be
able to use it to drop the database while you are in the same database.
Are you trying to run the query directly after connecting useing the
connection string just given. Or is there any "use Customer" before execute
ExecuteNonQuery().
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>|||Hello,
I have been doing some testing. I am filling a Dataset using a DataAdapter.
If I create a new connection to SQL Express without calling the
DataAdapter's Fill Method I can Drop the Database.
If I Fill the Dataset with the DataAdapter, dispose the connection and the
DataAdapter and create a new connection it still wont Drop the Database.
Even if the Dataset is empty it still does not work. It appears that
calling the DataAdapter before dropping the table keeps the database in use
weather it has finished it's job or not.
I can't even call a Stored Procedure to drop the Database if I call the
DataAdapter First. Even if I run the program and call the DataAdapter and
go into SQL Express Management Studio, I still get the same error. Only
when I shut down the program or drop the table without using the DataAdapter
does it work.
Is there a way around this?
Thanks
Chuck
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
Hello,
I am using SQL Express and Visual Studion 2003. I am trying to DROP
DATABASE Customers using a Stored Procedure and also using ADO.NET.
Query:
IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
DATABASE [Customers]
ConnectionString:
RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
Password=MyPassword;Initial Catalog=master"
Everytime I call the procedure or ExecuteNonQuery I get:
Cannot Drop Database 'Customers' because database is currently in use. by
sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
I have no problems Dropping the Tables.
Thanks,
Chuck|||The most likely cause is that the closed/disposed connection is pooled and
the pooled connection is preventing you from dropping the database. You can
specify Pooling=False in the connection string (or similar, depending on the
provider you are using) to disable connection pooling.
Another method is to execute "ALTER DATABASE MyDatabase SET SINGLE_USER WITH
ROLLBACK IMMEDIATE" to kill all connections to the database befor the drop.
Hope this helps.
Dan Guzman
SQL Server MVP
"Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
news:OgZmXqz9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I have been doing some testing. I am filling a Dataset using a
> DataAdapter.
> If I create a new connection to SQL Express without calling the
> DataAdapter's Fill Method I can Drop the Database.
> If I Fill the Dataset with the DataAdapter, dispose the connection and the
> DataAdapter and create a new connection it still wont Drop the Database.
> Even if the Dataset is empty it still does not work. It appears that
> calling the DataAdapter before dropping the table keeps the database in
> use
> weather it has finished it's job or not.
> I can't even call a Stored Procedure to drop the Database if I call the
> DataAdapter First. Even if I run the program and call the DataAdapter and
> go into SQL Express Management Studio, I still get the same error. Only
> when I shut down the program or drop the table without using the
> DataAdapter
> does it work.
> Is there a way around this?
> Thanks
> Chuck
>
> "Charles A. Lackman" <Charles@.CreateItSoftware.net> wrote in message
> news:epQ433v9GHA.1172@.TK2MSFTNGP03.phx.gbl...
> Hello,
> I am using SQL Express and Visual Studion 2003. I am trying to DROP
> DATABASE Customers using a Stored Procedure and also using ADO.NET.
> Query:
> IF EXISTS (SELECT name FROM sys.databases WHERE name = Customers) DROP
> DATABASE [Customers]
> ConnectionString:
> RemoveConnectionString = "Server = MyComputer\SQLExpress;UID=sa;
> Password=MyPassword;Initial Catalog=master"
>
> Everytime I call the procedure or ExecuteNonQuery I get:
> Cannot Drop Database 'Customers' because database is currently in use. by
> sqlclient.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> I have no problems Dropping the Tables.
> Thanks,
> Chuck
>
>

Friday, March 9, 2012

Open a TEXTE file with SQL server (stored procedure)

Hi,
I would like to open a *.log file (TEXTE format) from un stored
procedure SQL SERVER. After that, i just want to read each lines and
insert each line in a sql server table.
With that, i'll could schedule this stored procedure in the jobs of
sql server
Thant a lot for your help.
Alex.Have a look at BULK INSERT in BOL
--
HTH
Ryan Waight, MCDBA, MCSE
"alexfayard" <alexfayard@.hotmail.com> wrote in message
news:e3ed656a.0309170523.3b897e91@.posting.google.com...
> Hi,
> I would like to open a *.log file (TEXTE format) from un stored
> procedure SQL SERVER. After that, i just want to read each lines and
> insert each line in a sql server table.
> With that, i'll could schedule this stored procedure in the jobs of
> sql server
> Thant a lot for your help.
> Alex.