Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Sunday, March 25, 2012

Calling SP

Hi,
Can I call SP in the Trigger? Googled for this but din gt any satisfactory answer.
Thnkx,
Rahul JhaRahul, have you TRIED this yourself?|||I tried doing it georgy, but din get any error or warning message...... infact it executed successfully. I gt confused when i saw few blogs on the same saying that an stored procedure can't be called from the trigger...... hence thought of to put across to the trusted people on the forum.

thnkx,
rahul jha|||Fancy posting the code you tried?|||This may be the wrong thread to ask this question on, but how does one go about requesting a userid change on this website? ;)|||As far as I know, it can be done (never tried it myself), but it tends to be a bad idea for performance reasons. You want the trigger to be as fast as possible, and not call any intricate processing that can take even tenths of seconds. Think of it this way, If you have a trigger that takes 0.1 seconds to run, it sounds good...until you try to insert 100 records (a very modest amount).|||Here is the code that is calling the trigger.

CREATE PROCEDURE [dbo].[insert_tblCustomer_EDDCategory]
AS
BEGIN
INSERT INTO tblCustomer_EDDCategory VALUES (1, 2, 'Casino and Bingo Operations', 7, getdate(), NULL, '1.00')
END

and this the code that executes the sp from trigger

EXEC uspFetchAO|||And what does "uspFetchAO" do?|||there is just a select statement|||and even if there is a INSERT statement in the called SP, it works. i.e. the record gets inserted into the table.|||And finally...
The code for the TRIGGER please.

EDIT: Your trigger just runs a select statement?!
what on earth?!|||You did not get my point Georgy. Let me write it in a step wise manner...

1. I have a Stored Proc SP1 that INSERT a recod in table T1.
2. Table T1 has a Trigger (After Insert), say TR1
3. Trigger TR1 INSERT record in table T2
4. Trigger TR1 has a statement that EXECUTE a Stored Proc SP2
5. Stored Proc SP2 INSERT a record in table T3 and has a SELECT statement as well.
6. Now when I EXECUTE SP1, there is no error message and all the tables (T1, T2 & T3) has new records.

i.e. Stored Proc can be called from a Trigger?

My experiment say 'Yes' Georgy. But the blogs on the net and one of the thread in dbforum say 'NO'. And here i got confused...... and thus knocked the door of the FORUM.......

OVER.....

Thanks,
Rahul Jha|||If the question is
"Can you call a sproc from a trigger" then the answer is yes.
Proof:

CREATE TABLE gvTest (
Field0 int IDENTITY(1,1)
, Field1 char(4)
, Field2 char(4)
, FieldN char(4)
)
GO

CREATE TRIGGER gvTrig
ON gvTest
FOR INSERT
AS
SELECT 'Look, the INSERT trigger function worked!'
GO

CREATE PROCEDURE gvProc
AS
SELECT 'Look, the procedure worked!'
GO

CREATE TRIGGER gvTrigProc
ON gvTest
FOR UPDATE
AS
EXEC gvProc
GO

INSERT INTO gvTest(Field1) VALUES ('xxxx')

UPDATE gvTest
SET Field2 = 'yyyy'

DROP PROCEDURE gvProc
DROP TRIGGER gvTrigProc
DROP TRIGGER gvTrig
DROP TABLE gvTest

If the question is
"Should I call sprocs from triggers" then the answer is further up this page.|||Georgy, This was my question...........

Hi,
Can I call SP in the Trigger? Googled for this but din gt any satisfactory answer.

Thnkx,
Rahul Jha

Thursday, March 22, 2012

Calling native code from a trigger.

I've been searching for a bit but I can't seem to find a difinitive answer.

Can an SQL Server trigger call native C/C++ functions? If so, what is the mechanism?

Thanks in advance.

My knee-jerk reaction is that this sounds like a very bad idea, but perhaps not. What are you trying to accomplish?|||Moving thread to the more appropriate forum.|||

You could do that by creating a CLR procedure or function that wrapped the Win32 API calls.

What are you attemping to accomplish?

|||I was thinking of having a trigger that would call a native function that could do some needed tasks outside the SQL server. Things like post a message to an open mail slot on either the same or a different server so that another service can perform some work. After talking with on of the other developers, we will probably send the messages from his app. that is connected to the SQL server. I was thinking of having a trigger do it because it would remove him from having to manage the messages. Also a thought for future development.|||

Personally, I wouldn't have a TRIGGER engage any outside service. It provides too many additional surfaces for disruption -the TRIGGER hangs waiting for the outside service ...

A better approach is to have the TRIGGER insert the relevant data into a queueing table.

Then have a separate SQL Agent Job that constantly runs checking the queue, and upon finding a row of data, processes that row in whatever fashion is appropriate (sending mail, logging, etc.)

|||

In my case, it would just post a message into a queue and a thread would be watching the queue. The processing would happen on another process or service running multi threaded code. The trigger would probably return in a few ms. This would probably be faster than a lot of ODBC calls and the other service can simply wait on an event using no CPU cycles until an event is signalled.

Different ways to skin the cat I guess. Coders like to have code do the work and DBA's like the DBMS to do the work. Also a factor of what you are comfortable with.

Calling native code from a trigger.

I've been searching for a bit but I can't seem to find a difinitive answer.

Can an SQL Server trigger call native C/C++ functions? If so, what is the mechanism?

Thanks in advance.

My knee-jerk reaction is that this sounds like a very bad idea, but perhaps not. What are you trying to accomplish?|||Moving thread to the more appropriate forum.|||

You could do that by creating a CLR procedure or function that wrapped the Win32 API calls.

What are you attemping to accomplish?

|||I was thinking of having a trigger that would call a native function that could do some needed tasks outside the SQL server. Things like post a message to an open mail slot on either the same or a different server so that another service can perform some work. After talking with on of the other developers, we will probably send the messages from his app. that is connected to the SQL server. I was thinking of having a trigger do it because it would remove him from having to manage the messages. Also a thought for future development.|||

Personally, I wouldn't have a TRIGGER engage any outside service. It provides too many additional surfaces for disruption -the TRIGGER hangs waiting for the outside service ...

A better approach is to have the TRIGGER insert the relevant data into a queueing table.

Then have a separate SQL Agent Job that constantly runs checking the queue, and upon finding a row of data, processes that row in whatever fashion is appropriate (sending mail, logging, etc.)

|||

In my case, it would just post a message into a queue and a thread would be watching the queue. The processing would happen on another process or service running multi threaded code. The trigger would probably return in a few ms. This would probably be faster than a lot of ODBC calls and the other service can simply wait on an event using no CPU cycles until an event is signalled.

Different ways to skin the cat I guess. Coders like to have code do the work and DBA's like the DBMS to do the work. Also a factor of what you are comfortable with.

sql

Tuesday, March 20, 2012

Calling External App From Sproc or Trigger?

Hi all. Yes, it's me again. LOL

My question is this: Can I program something in the way of a trigger or a sproc that when a certain field is populated (on insert/update) that it calls a VB application or some other external program (possibly a web app?)

If not, does anyone have any suggestions on how I might accomplish this for SQL7? We have actually begun testing to upgrade our 7DBs to 2K, so if it's possible to do something like this in 2K that will also work for me.

Thanks for all the help you guys provide me! I need every bit of it sometimes!

Dirkuse xp_cmdshell in your trigger.

"tnjazzgrass" wrote in message
news:2430444.1043362868@.dbforums.com...
> Hi all. Yes, it's me again. LOL
> My question is this: Can I program something in the way of a trigger
> or a sproc that when a certain field is populated (on insert/update)
> that it calls a VB application or some other external program (possibly
> a web app?)
> If not, does anyone have any suggestions on how I might accomplish
> this for SQL7? We have actually begun testing to upgrade our 7DBs to
> 2K, so if it's possible to do something like this in 2K that will also
> work for me.
> Thanks for all the help you guys provide me! I need every bit of it
> sometimes!
> Dirk
> --
> Posted via http://dbforums.com (http://dbforums.com/)|||As Radzi says you should use the system extended sored procedure xp_cmdshell within a trigger. Something like this will do the trick:

CREATE TRIGGER tr_myTrigger ON dbo.myTable
FOR INSERT, UPDATE, DELETE
AS

DECLARE @.Cmd varchar(1000)
BEGIN
SET @.Cmd = 'c:\..\program.exe'
EXEC master..xp_cmdshell @.Cmd, NO_OUTPUT
END

Cheers!sql

Monday, March 19, 2012

Calling a webservice from a SQL Trigger

Please,
is there a way i can execute a webservice from within a SQL Server 2k
Trigger? Sending and receiving XML values?
Tks,
Fabiano
Hi
Maybe you can do this through a COM object and the sp_OA* procedures?
John
"Fabiano" <a@.a.com.br> wrote in message
news:%23WqHeXqlEHA.1644@.tk2msftngp13.phx.gbl...
> Please,
> is there a way i can execute a webservice from within a SQL Server 2k
> Trigger? Sending and receiving XML values?
> Tks,
> Fabiano
>
>

Calling a webservice from a SQL Trigger

Please,
is there a way i can execute a webservice from within a SQL Server 2k
Trigger? Sending and receiving XML values?
Tks,
FabianoHi
Maybe you can do this through a COM object and the sp_OA* procedures?
John
"Fabiano" <a@.a.com.br> wrote in message
news:%23WqHeXqlEHA.1644@.tk2msftngp13.phx.gbl...
> Please,
> is there a way i can execute a webservice from within a SQL Server 2k
> Trigger? Sending and receiving XML values?
> Tks,
> Fabiano
>
>

Thursday, March 8, 2012

Call webservice from trigger/stored proc

Is it possible to call an external web service from a SQL Server trigger or stored procedure?You can sort of call a web service from a stored procedure, but it takes a lot of bubble gum and bailing wire to make it work. Can you describe what you want in a bit more detail, and one of us might have an idea or three that might help.

-PatP|||Hi
I want to call the web services which is developed in .net from sqlserver 2000.So please do help me.|||Here is a sample code (without error handling):

declare @.objRef int
,@.resultcode int
,@.responseText varchar(8000)

EXEC @.resultcode = sp_OACreate 'MSXML2.ServerXMLHTTP', @.objRef OUT,4
EXEC @.resultcode = sp_OAMethod @.objRef, 'Open', NULL,'GET','http://myurl.com', False
EXEC @.resultcode = sp_OAMethod @.objRef, 'Send',null
EXEC @.resultcode = sp_OAGetProperty @.objRef, 'responseText',@.responseText output

print(@.response)

EXEC sp_OADestroy @.objRef

Call Web Service from the database

Hi,
I have a situation where I need to call a web service from the database (by an insert trigger on a table). Please let me know how I could do this.
Thanks in advance
RajSame question (http://www.dbforums.com/t996377.html), same solution.

-PatP

Call trigger when new trigger is created

Is it at all possible to call a trigger when a new trigger is created?If you are using SQL Server 2005, take a look at DDL triggers.|||

Triggers are invoked by the engine based on certain actions (DML and DDL & others in SQL Server 2005). So if you are on SQL Server 2005, you can use a DDL trigger to create one for CREATE TRIGGER action. This will fire the DDL trigger. There is no way to explicitly fire a trigger (by name) without invoking the action that causes the trigger to be fired. See links below for more details on DDL triggers in SQL Server 2005:

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

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

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

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

You can also create event notifications in SQL Server 2005 for DDL actions, receive message in a service broker queue which can invoke a SP that performs your logic. But this is more elaborate mechanism and it depends on your needs.

|||

Thank you for the information. I am trying to write the trigger, but I get an error...

Line 3: Incorrect syntax near 'CREATE_TRIGGER'.

Here is what I have for my trigger:

CREATE TRIGGER WriteReg

ON dbo.Toc

FOR CREATE_TRIGGER

AS

EXEC master ..xp_cmdshell "C:\Path\Name.exe"

GO

Does anyone have any ideas on what I need to do differently? Is it because I'm using SQL Server 2005 Express, not the full edition?

Thanks

|||Please check the Books Online for complete syntax of DDL triggers. DDL triggers operate at server or database scope. You cannot specify an object explicitly like DML triggers. So the ON clause should be either DATABASE or ALL SERVER. To check for specific objects, you would use the EVENTDATA() value available inside the trigger.|||I have tried using DATABASE instead of the table name, but it gets an error saying "Incorrect syntax near keyword 'DATABASE'". Any ideas why? Could it be because I'm using the Express edition of SQL Server 2005?|||

Below works for me:

Code Snippet

create trigger TrapCreateTrg
on database
after CREATE_TRIGGER
AS
return;

Please post the code that doesn't work for you.

|||I have desided to go with a different route. I created a new table to track information about my application and I put a trigger on that table.

call to xp_cmdshell from trigger problem

To any SQL Server MVP:
We have a problem that out group can not seem to resolve. We have a
database that is used to alert and calculate required amounts of assistance
to clients that monitor through a private frame relay WAN. When a problem
occurs client 1 enters the request at a workstation which updates the
database and causes a trigger to fire and starts a process to notify the
other clients that assistance is required. This portion has been in use for
several years but now need to send information to a control area system,
rather than single entity, that is in XML format so a program was created to
take the data and format in accordance to the XML Schema rules the area
used.
At first it failed because the trigger called the EXE but the tables were
locked by the first program called in the trigger. To resolve this and
allow the trigger to continue thus unlocking the tables we changed the
trigger to call a vbs script file rather than the EXE directly. This seemed
to resolve the situation with locked tables in the database but a new issue
has come up I can not figure out.
The EXE has to use a X509 digital certificate to pass the data to the
client's server. When I run the EXE from the command line it works, when I
use SQL Query Manager and run the trigger code (less the alert portion) it
works but when the trigger fires is fails to send the information. It builds
everything but the client never gets the XML stream? We currently use
simple text files to track where the program process is but this file
indicates it finishes properly and all cleanup of objects occurs (Code in
VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
indicate within the text file who started the program and this indicates the
"SQLProxy" account is who is running the EXE file. It almost seems that when
impersonate process occurs the local server does not treat the SQLProxy
account as the right one?
How can we test using SQL Query Manager the exact process as when the
trigger is fired from SQL Server?
Information:
ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called from
the trigger by the application user. This is what we created as instructed
by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
works because the first step in the trigger works.
CERTIFICATE: We logged on to the server using SQLProxy so the
certificate could be installed into the "My" store for the user. We could
move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
Hi Daniel,
I am just wondering under what user you did your test on query analyzer.
Have you tried the following to see what kind of result you get:
1. log in on the server by using your SQLProxy
2. Open query analyzer and connect to the server with Windows Authentication
3. try a test and see if the client gets the required info.
Sasan Saidi
Senior DBA
"Daniel Gard" wrote:

> To any SQL Server MVP:
> We have a problem that out group can not seem to resolve. We have a
> database that is used to alert and calculate required amounts of assistance
> to clients that monitor through a private frame relay WAN. When a problem
> occurs client 1 enters the request at a workstation which updates the
> database and causes a trigger to fire and starts a process to notify the
> other clients that assistance is required. This portion has been in use for
> several years but now need to send information to a control area system,
> rather than single entity, that is in XML format so a program was created to
> take the data and format in accordance to the XML Schema rules the area
> used.
> At first it failed because the trigger called the EXE but the tables were
> locked by the first program called in the trigger. To resolve this and
> allow the trigger to continue thus unlocking the tables we changed the
> trigger to call a vbs script file rather than the EXE directly. This seemed
> to resolve the situation with locked tables in the database but a new issue
> has come up I can not figure out.
> The EXE has to use a X509 digital certificate to pass the data to the
> client's server. When I run the EXE from the command line it works, when I
> use SQL Query Manager and run the trigger code (less the alert portion) it
> works but when the trigger fires is fails to send the information. It builds
> everything but the client never gets the XML stream? We currently use
> simple text files to track where the program process is but this file
> indicates it finishes properly and all cleanup of objects occurs (Code in
> VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
> indicate within the text file who started the program and this indicates the
> "SQLProxy" account is who is running the EXE file. It almost seems that when
> impersonate process occurs the local server does not treat the SQLProxy
> account as the right one?
> How can we test using SQL Query Manager the exact process as when the
> trigger is fired from SQL Server?
>
> Information:
> ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called from
> the trigger by the application user. This is what we created as instructed
> by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> works because the first step in the trigger works.
> CERTIFICATE: We logged on to the server using SQLProxy so the
> certificate could be installed into the "My" store for the user. We could
> move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
>
>
>
>
>
|||Sasan
We have tried all of the following:
1. Logged on to the Windows server as the Domain Administrator and
connecting to SQL Server using windows authentication.
2. Logged on to the Windows server as the SQLProxy Account and connecting
to SQL Server using windows authentication.
We did notice is logged on to the Windows server as either 1 or 2 and
logging onto the SQL Server as the application user it does not work.
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication[vbcol=seagreen]
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
assistance[vbcol=seagreen]
problem[vbcol=seagreen]
for[vbcol=seagreen]
created to[vbcol=seagreen]
were[vbcol=seagreen]
seemed[vbcol=seagreen]
issue[vbcol=seagreen]
when I[vbcol=seagreen]
it[vbcol=seagreen]
builds[vbcol=seagreen]
in[vbcol=seagreen]
to[vbcol=seagreen]
the[vbcol=seagreen]
when[vbcol=seagreen]
from[vbcol=seagreen]
instructed[vbcol=seagreen]
could[vbcol=seagreen]
|||Sasan
The application user is a SQL Server account and not a Windows user account.
We noticed the following behavior. If the application user account was
placed into the SQL System Administrator group the program runsa as the
Domain account for the SQL Server and if this user is not a in the
administrator group then the program runs as SQLProxy.
The more I mess with this the more I think the user running the application
after the call from SQL Server can not find the digital certificate in the
CURRENT_USER\My store. How does the impersonation really work with SQL
Server?
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication[vbcol=seagreen]
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
assistance[vbcol=seagreen]
problem[vbcol=seagreen]
for[vbcol=seagreen]
created to[vbcol=seagreen]
were[vbcol=seagreen]
seemed[vbcol=seagreen]
issue[vbcol=seagreen]
when I[vbcol=seagreen]
it[vbcol=seagreen]
builds[vbcol=seagreen]
in[vbcol=seagreen]
to[vbcol=seagreen]
the[vbcol=seagreen]
when[vbcol=seagreen]
from[vbcol=seagreen]
instructed[vbcol=seagreen]
could[vbcol=seagreen]
|||When xp_cmdshell is invoked by a user who is a member of the sysadmin fixed
server role, xp_cmdshell will be executed under the security context in which
the SQL Server service is running. When the user is not a member of the
sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
account, which is specified using xp_sqlagent_proxy_account. If the proxy
account is not available, xp_cmdshell will fail.
Sasan
"Daniel Gard" wrote:

> Sasan
> The application user is a SQL Server account and not a Windows user account.
> We noticed the following behavior. If the application user account was
> placed into the SQL System Administrator group the program runsa as the
> Domain account for the SQL Server and if this user is not a in the
> administrator group then the program runs as SQLProxy.
> The more I mess with this the more I think the user running the application
> after the call from SQL Server can not find the digital certificate in the
> CURRENT_USER\My store. How does the impersonation really work with SQL
> Server?
> Thanks
> Dan
>
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Authentication
> assistance
> problem
> for
> created to
> were
> seemed
> issue
> when I
> it
> builds
> in
> to
> the
> when
> from
> instructed
> could
>
>
|||Sasan
I understand that and we have all of this working. The problem seems very
strange even in my book because I am not sure if the impersonation process
uses a modified SID from the one if you log on the the server using the same
account. Does it take the domain user and create a temporary local account?
If so then that is the problem and this user can not find the certificate.
We are going to try to use a public certificate store and see if the process
will complete.
The program we wrote runs no matter what method the difference is when run
from the command line or from SQL Query manager it finishes by sending XML
to the the client but if the same process runs from being called by the vbs
script that is called by the trigger the client get nothing.
Thanks
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> When xp_cmdshell is invoked by a user who is a member of the sysadmin
fixed
> server role, xp_cmdshell will be executed under the security context in
which[vbcol=seagreen]
> the SQL Server service is running. When the user is not a member of the
> sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
> account, which is specified using xp_sqlagent_proxy_account. If the proxy
> account is not available, xp_cmdshell will fail.
> Sasan
> "Daniel Gard" wrote:
account.[vbcol=seagreen]
application[vbcol=seagreen]
the[vbcol=seagreen]
analyzer.[vbcol=seagreen]
the[vbcol=seagreen]
the[vbcol=seagreen]
use[vbcol=seagreen]
system,[vbcol=seagreen]
area[vbcol=seagreen]
and[vbcol=seagreen]
the[vbcol=seagreen]
This[vbcol=seagreen]
new[vbcol=seagreen]
the[vbcol=seagreen]
works,[vbcol=seagreen]
portion)[vbcol=seagreen]
It[vbcol=seagreen]
use[vbcol=seagreen]
file[vbcol=seagreen]
(Code[vbcol=seagreen]
altered[vbcol=seagreen]
indicates[vbcol=seagreen]
that[vbcol=seagreen]
SQLProxy[vbcol=seagreen]
the[vbcol=seagreen]
called[vbcol=seagreen]
it[vbcol=seagreen]
|||Daniel, I am not sure if this can help in any ways but you might want to have
a quick look at it:
Article: Setting the Default Process Security Level Using VBScript
Link:
http://msdn.microsoft.com/library/de...v bscript.asp
Sasan
"Daniel Gard" wrote:

> Sasan
> I understand that and we have all of this working. The problem seems very
> strange even in my book because I am not sure if the impersonation process
> uses a modified SID from the one if you log on the the server using the same
> account. Does it take the domain user and create a temporary local account?
> If so then that is the problem and this user can not find the certificate.
> We are going to try to use a public certificate store and see if the process
> will complete.
> The program we wrote runs no matter what method the difference is when run
> from the command line or from SQL Query manager it finishes by sending XML
> to the the client but if the same process runs from being called by the vbs
> script that is called by the trigger the client get nothing.
> Thanks
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> fixed
> which
> account.
> application
> the
> analyzer.
> the
> the
> use
> system,
> area
> and
> the
> This
> new
> the
> works,
> portion)
> It
> use
> file
> (Code
> altered
> indicates
> that
> SQLProxy
> the
> called
> it
>
>
|||Sasan,
I'll look this over maybe there is something we missed in the vbs script
being called by the trigger.
Thank You,
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:BF67C18C-AEEC-408D-9185-86D4B5DC0772@.microsoft.com...
> Daniel, I am not sure if this can help in any ways but you might want to
have
> a quick look at it:
> Article: Setting the Default Process Security Level Using VBScript
> Link:
>
http://msdn.microsoft.com/library/de...v bscript.asp[vbcol=seagreen]
>
> Sasan
> "Daniel Gard" wrote:
very[vbcol=seagreen]
process[vbcol=seagreen]
same[vbcol=seagreen]
account?[vbcol=seagreen]
certificate.[vbcol=seagreen]
process[vbcol=seagreen]
run[vbcol=seagreen]
XML[vbcol=seagreen]
vbs[vbcol=seagreen]
in[vbcol=seagreen]
the[vbcol=seagreen]
proxy[vbcol=seagreen]
proxy[vbcol=seagreen]
was[vbcol=seagreen]
the[vbcol=seagreen]
in[vbcol=seagreen]
SQL[vbcol=seagreen]
message[vbcol=seagreen]
have a[vbcol=seagreen]
a[vbcol=seagreen]
updates[vbcol=seagreen]
notify[vbcol=seagreen]
been in[vbcol=seagreen]
was[vbcol=seagreen]
the[vbcol=seagreen]
tables[vbcol=seagreen]
this[vbcol=seagreen]
changed[vbcol=seagreen]
a[vbcol=seagreen]
to[vbcol=seagreen]
information.[vbcol=seagreen]
currently[vbcol=seagreen]
seems[vbcol=seagreen]
when[vbcol=seagreen]
know[vbcol=seagreen]
the[vbcol=seagreen]
We[vbcol=seagreen]
should.[vbcol=seagreen]

call to xp_cmdshell from trigger problem

To any SQL Server MVP:
We have a problem that out group can not seem to resolve. We have a
database that is used to alert and calculate required amounts of assistance
to clients that monitor through a private frame relay WAN. When a problem
occurs client 1 enters the request at a workstation which updates the
database and causes a trigger to fire and starts a process to notify the
other clients that assistance is required. This portion has been in use for
several years but now need to send information to a control area system,
rather than single entity, that is in XML format so a program was created to
take the data and format in accordance to the XML Schema rules the area
used.
At first it failed because the trigger called the EXE but the tables were
locked by the first program called in the trigger. To resolve this and
allow the trigger to continue thus unlocking the tables we changed the
trigger to call a vbs script file rather than the EXE directly. This seemed
to resolve the situation with locked tables in the database but a new issue
has come up I can not figure out.
The EXE has to use a X509 digital certificate to pass the data to the
client's server. When I run the EXE from the command line it works, when I
use SQL Query Manager and run the trigger code (less the alert portion) it
works but when the trigger fires is fails to send the information. It builds
everything but the client never gets the XML stream? We currently use
simple text files to track where the program process is but this file
indicates it finishes properly and all cleanup of objects occurs (Code in
VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
indicate within the text file who started the program and this indicates the
"SQLProxy" account is who is running the EXE file. It almost seems that when
impersonate process occurs the local server does not treat the SQLProxy
account as the right one?
How can we test using SQL Query Manager the exact process as when the
trigger is fired from SQL Server?
Information:
ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called from
the trigger by the application user. This is what we created as instructed
by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
works because the first step in the trigger works.
CERTIFICATE: We logged on to the server using SQLProxy so the
certificate could be installed into the "My" store for the user. We could
move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.Hi Daniel,
I am just wondering under what user you did your test on query analyzer.
Have you tried the following to see what kind of result you get:
1. log in on the server by using your SQLProxy
2. Open query analyzer and connect to the server with Windows Authentication
3. try a test and see if the client gets the required info.
Sasan Saidi
Senior DBA
"Daniel Gard" wrote:
> To any SQL Server MVP:
> We have a problem that out group can not seem to resolve. We have a
> database that is used to alert and calculate required amounts of assistance
> to clients that monitor through a private frame relay WAN. When a problem
> occurs client 1 enters the request at a workstation which updates the
> database and causes a trigger to fire and starts a process to notify the
> other clients that assistance is required. This portion has been in use for
> several years but now need to send information to a control area system,
> rather than single entity, that is in XML format so a program was created to
> take the data and format in accordance to the XML Schema rules the area
> used.
> At first it failed because the trigger called the EXE but the tables were
> locked by the first program called in the trigger. To resolve this and
> allow the trigger to continue thus unlocking the tables we changed the
> trigger to call a vbs script file rather than the EXE directly. This seemed
> to resolve the situation with locked tables in the database but a new issue
> has come up I can not figure out.
> The EXE has to use a X509 digital certificate to pass the data to the
> client's server. When I run the EXE from the command line it works, when I
> use SQL Query Manager and run the trigger code (less the alert portion) it
> works but when the trigger fires is fails to send the information. It builds
> everything but the client never gets the XML stream? We currently use
> simple text files to track where the program process is but this file
> indicates it finishes properly and all cleanup of objects occurs (Code in
> VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
> indicate within the text file who started the program and this indicates the
> "SQLProxy" account is who is running the EXE file. It almost seems that when
> impersonate process occurs the local server does not treat the SQLProxy
> account as the right one?
> How can we test using SQL Query Manager the exact process as when the
> trigger is fired from SQL Server?
>
> Information:
> ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called from
> the trigger by the application user. This is what we created as instructed
> by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> works because the first step in the trigger works.
> CERTIFICATE: We logged on to the server using SQLProxy so the
> certificate could be installed into the "My" store for the user. We could
> move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
>
>
>
>
>|||Sasan
We have tried all of the following:
1. Logged on to the Windows server as the Domain Administrator and
connecting to SQL Server using windows authentication.
2. Logged on to the Windows server as the SQLProxy Account and connecting
to SQL Server using windows authentication.
We did notice is logged on to the Windows server as either 1 or 2 and
logging onto the SQL Server as the application user it does not work.
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
> > To any SQL Server MVP:
> >
> > We have a problem that out group can not seem to resolve. We have a
> > database that is used to alert and calculate required amounts of
assistance
> > to clients that monitor through a private frame relay WAN. When a
problem
> > occurs client 1 enters the request at a workstation which updates the
> > database and causes a trigger to fire and starts a process to notify the
> > other clients that assistance is required. This portion has been in use
for
> > several years but now need to send information to a control area system,
> > rather than single entity, that is in XML format so a program was
created to
> > take the data and format in accordance to the XML Schema rules the area
> > used.
> >
> > At first it failed because the trigger called the EXE but the tables
were
> > locked by the first program called in the trigger. To resolve this and
> > allow the trigger to continue thus unlocking the tables we changed the
> > trigger to call a vbs script file rather than the EXE directly. This
seemed
> > to resolve the situation with locked tables in the database but a new
issue
> > has come up I can not figure out.
> >
> > The EXE has to use a X509 digital certificate to pass the data to the
> > client's server. When I run the EXE from the command line it works,
when I
> > use SQL Query Manager and run the trigger code (less the alert portion)
it
> > works but when the trigger fires is fails to send the information. It
builds
> > everything but the client never gets the XML stream? We currently use
> > simple text files to track where the program process is but this file
> > indicates it finishes properly and all cleanup of objects occurs (Code
in
> > VB6 and we use WinHTTP Request Object 5.1). The code has been altered
to
> > indicate within the text file who started the program and this indicates
the
> > "SQLProxy" account is who is running the EXE file. It almost seems that
when
> > impersonate process occurs the local server does not treat the SQLProxy
> > account as the right one?
> >
> > How can we test using SQL Query Manager the exact process as when the
> > trigger is fired from SQL Server?
> >
> >
> > Information:
> >
> > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called
from
> > the trigger by the application user. This is what we created as
instructed
> > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> > works because the first step in the trigger works.
> >
> > CERTIFICATE: We logged on to the server using SQLProxy so the
> > certificate could be installed into the "My" store for the user. We
could
> > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
> >
> >
> >
> >
> >
> >
> >
> >
> >|||Sasan
The application user is a SQL Server account and not a Windows user account.
We noticed the following behavior. If the application user account was
placed into the SQL System Administrator group the program runsa as the
Domain account for the SQL Server and if this user is not a in the
administrator group then the program runs as SQLProxy.
The more I mess with this the more I think the user running the application
after the call from SQL Server can not find the digital certificate in the
CURRENT_USER\My store. How does the impersonation really work with SQL
Server?
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
> > To any SQL Server MVP:
> >
> > We have a problem that out group can not seem to resolve. We have a
> > database that is used to alert and calculate required amounts of
assistance
> > to clients that monitor through a private frame relay WAN. When a
problem
> > occurs client 1 enters the request at a workstation which updates the
> > database and causes a trigger to fire and starts a process to notify the
> > other clients that assistance is required. This portion has been in use
for
> > several years but now need to send information to a control area system,
> > rather than single entity, that is in XML format so a program was
created to
> > take the data and format in accordance to the XML Schema rules the area
> > used.
> >
> > At first it failed because the trigger called the EXE but the tables
were
> > locked by the first program called in the trigger. To resolve this and
> > allow the trigger to continue thus unlocking the tables we changed the
> > trigger to call a vbs script file rather than the EXE directly. This
seemed
> > to resolve the situation with locked tables in the database but a new
issue
> > has come up I can not figure out.
> >
> > The EXE has to use a X509 digital certificate to pass the data to the
> > client's server. When I run the EXE from the command line it works,
when I
> > use SQL Query Manager and run the trigger code (less the alert portion)
it
> > works but when the trigger fires is fails to send the information. It
builds
> > everything but the client never gets the XML stream? We currently use
> > simple text files to track where the program process is but this file
> > indicates it finishes properly and all cleanup of objects occurs (Code
in
> > VB6 and we use WinHTTP Request Object 5.1). The code has been altered
to
> > indicate within the text file who started the program and this indicates
the
> > "SQLProxy" account is who is running the EXE file. It almost seems that
when
> > impersonate process occurs the local server does not treat the SQLProxy
> > account as the right one?
> >
> > How can we test using SQL Query Manager the exact process as when the
> > trigger is fired from SQL Server?
> >
> >
> > Information:
> >
> > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called
from
> > the trigger by the application user. This is what we created as
instructed
> > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> > works because the first step in the trigger works.
> >
> > CERTIFICATE: We logged on to the server using SQLProxy so the
> > certificate could be installed into the "My" store for the user. We
could
> > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
> >
> >
> >
> >
> >
> >
> >
> >
> >|||When xp_cmdshell is invoked by a user who is a member of the sysadmin fixed
server role, xp_cmdshell will be executed under the security context in which
the SQL Server service is running. When the user is not a member of the
sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
account, which is specified using xp_sqlagent_proxy_account. If the proxy
account is not available, xp_cmdshell will fail.
Sasan
"Daniel Gard" wrote:
> Sasan
> The application user is a SQL Server account and not a Windows user account.
> We noticed the following behavior. If the application user account was
> placed into the SQL System Administrator group the program runsa as the
> Domain account for the SQL Server and if this user is not a in the
> administrator group then the program runs as SQLProxy.
> The more I mess with this the more I think the user running the application
> after the call from SQL Server can not find the digital certificate in the
> CURRENT_USER\My store. How does the impersonation really work with SQL
> Server?
> Thanks
> Dan
>
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> > Hi Daniel,
> >
> > I am just wondering under what user you did your test on query analyzer.
> >
> > Have you tried the following to see what kind of result you get:
> > 1. log in on the server by using your SQLProxy
> > 2. Open query analyzer and connect to the server with Windows
> Authentication
> > 3. try a test and see if the client gets the required info.
> >
> > Sasan Saidi
> > Senior DBA
> >
> > "Daniel Gard" wrote:
> >
> > > To any SQL Server MVP:
> > >
> > > We have a problem that out group can not seem to resolve. We have a
> > > database that is used to alert and calculate required amounts of
> assistance
> > > to clients that monitor through a private frame relay WAN. When a
> problem
> > > occurs client 1 enters the request at a workstation which updates the
> > > database and causes a trigger to fire and starts a process to notify the
> > > other clients that assistance is required. This portion has been in use
> for
> > > several years but now need to send information to a control area system,
> > > rather than single entity, that is in XML format so a program was
> created to
> > > take the data and format in accordance to the XML Schema rules the area
> > > used.
> > >
> > > At first it failed because the trigger called the EXE but the tables
> were
> > > locked by the first program called in the trigger. To resolve this and
> > > allow the trigger to continue thus unlocking the tables we changed the
> > > trigger to call a vbs script file rather than the EXE directly. This
> seemed
> > > to resolve the situation with locked tables in the database but a new
> issue
> > > has come up I can not figure out.
> > >
> > > The EXE has to use a X509 digital certificate to pass the data to the
> > > client's server. When I run the EXE from the command line it works,
> when I
> > > use SQL Query Manager and run the trigger code (less the alert portion)
> it
> > > works but when the trigger fires is fails to send the information. It
> builds
> > > everything but the client never gets the XML stream? We currently use
> > > simple text files to track where the program process is but this file
> > > indicates it finishes properly and all cleanup of objects occurs (Code
> in
> > > VB6 and we use WinHTTP Request Object 5.1). The code has been altered
> to
> > > indicate within the text file who started the program and this indicates
> the
> > > "SQLProxy" account is who is running the EXE file. It almost seems that
> when
> > > impersonate process occurs the local server does not treat the SQLProxy
> > > account as the right one?
> > >
> > > How can we test using SQL Query Manager the exact process as when the
> > > trigger is fired from SQL Server?
> > >
> > >
> > > Information:
> > >
> > > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called
> from
> > > the trigger by the application user. This is what we created as
> instructed
> > > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> > > works because the first step in the trigger works.
> > >
> > > CERTIFICATE: We logged on to the server using SQLProxy so the
> > > certificate could be installed into the "My" store for the user. We
> could
> > > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
>
>|||Sasan
I understand that and we have all of this working. The problem seems very
strange even in my book because I am not sure if the impersonation process
uses a modified SID from the one if you log on the the server using the same
account. Does it take the domain user and create a temporary local account?
If so then that is the problem and this user can not find the certificate.
We are going to try to use a public certificate store and see if the process
will complete.
The program we wrote runs no matter what method the difference is when run
from the command line or from SQL Query manager it finishes by sending XML
to the the client but if the same process runs from being called by the vbs
script that is called by the trigger the client get nothing.
Thanks
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> When xp_cmdshell is invoked by a user who is a member of the sysadmin
fixed
> server role, xp_cmdshell will be executed under the security context in
which
> the SQL Server service is running. When the user is not a member of the
> sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
> account, which is specified using xp_sqlagent_proxy_account. If the proxy
> account is not available, xp_cmdshell will fail.
> Sasan
> "Daniel Gard" wrote:
> > Sasan
> >
> > The application user is a SQL Server account and not a Windows user
account.
> > We noticed the following behavior. If the application user account was
> > placed into the SQL System Administrator group the program runsa as the
> > Domain account for the SQL Server and if this user is not a in the
> > administrator group then the program runs as SQLProxy.
> >
> > The more I mess with this the more I think the user running the
application
> > after the call from SQL Server can not find the digital certificate in
the
> > CURRENT_USER\My store. How does the impersonation really work with SQL
> > Server?
> >
> > Thanks
> > Dan
> >
> >
> > "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> > news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> > > Hi Daniel,
> > >
> > > I am just wondering under what user you did your test on query
analyzer.
> > >
> > > Have you tried the following to see what kind of result you get:
> > > 1. log in on the server by using your SQLProxy
> > > 2. Open query analyzer and connect to the server with Windows
> > Authentication
> > > 3. try a test and see if the client gets the required info.
> > >
> > > Sasan Saidi
> > > Senior DBA
> > >
> > > "Daniel Gard" wrote:
> > >
> > > > To any SQL Server MVP:
> > > >
> > > > We have a problem that out group can not seem to resolve. We have a
> > > > database that is used to alert and calculate required amounts of
> > assistance
> > > > to clients that monitor through a private frame relay WAN. When a
> > problem
> > > > occurs client 1 enters the request at a workstation which updates
the
> > > > database and causes a trigger to fire and starts a process to notify
the
> > > > other clients that assistance is required. This portion has been in
use
> > for
> > > > several years but now need to send information to a control area
system,
> > > > rather than single entity, that is in XML format so a program was
> > created to
> > > > take the data and format in accordance to the XML Schema rules the
area
> > > > used.
> > > >
> > > > At first it failed because the trigger called the EXE but the tables
> > were
> > > > locked by the first program called in the trigger. To resolve this
and
> > > > allow the trigger to continue thus unlocking the tables we changed
the
> > > > trigger to call a vbs script file rather than the EXE directly.
This
> > seemed
> > > > to resolve the situation with locked tables in the database but a
new
> > issue
> > > > has come up I can not figure out.
> > > >
> > > > The EXE has to use a X509 digital certificate to pass the data to
the
> > > > client's server. When I run the EXE from the command line it
works,
> > when I
> > > > use SQL Query Manager and run the trigger code (less the alert
portion)
> > it
> > > > works but when the trigger fires is fails to send the information.
It
> > builds
> > > > everything but the client never gets the XML stream? We currently
use
> > > > simple text files to track where the program process is but this
file
> > > > indicates it finishes properly and all cleanup of objects occurs
(Code
> > in
> > > > VB6 and we use WinHTTP Request Object 5.1). The code has been
altered
> > to
> > > > indicate within the text file who started the program and this
indicates
> > the
> > > > "SQLProxy" account is who is running the EXE file. It almost seems
that
> > when
> > > > impersonate process occurs the local server does not treat the
SQLProxy
> > > > account as the right one?
> > > >
> > > > How can we test using SQL Query Manager the exact process as when
the
> > > > trigger is fired from SQL Server?
> > > >
> > > >
> > > > Information:
> > > >
> > > > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be
called
> > from
> > > > the trigger by the application user. This is what we created as
> > instructed
> > > > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know
it
> > > > works because the first step in the trigger works.
> > > >
> > > > CERTIFICATE: We logged on to the server using SQLProxy so the
> > > > certificate could be installed into the "My" store for the user. We
> > could
> > > > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> >
> >
> >|||Daniel, I am not sure if this can help in any ways but you might want to have
a quick look at it:
Article: Setting the Default Process Security Level Using VBScript
Link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/setting_the_default_process_security_level_using_vbscript.asp
Sasan
"Daniel Gard" wrote:
> Sasan
> I understand that and we have all of this working. The problem seems very
> strange even in my book because I am not sure if the impersonation process
> uses a modified SID from the one if you log on the the server using the same
> account. Does it take the domain user and create a temporary local account?
> If so then that is the problem and this user can not find the certificate.
> We are going to try to use a public certificate store and see if the process
> will complete.
> The program we wrote runs no matter what method the difference is when run
> from the command line or from SQL Query manager it finishes by sending XML
> to the the client but if the same process runs from being called by the vbs
> script that is called by the trigger the client get nothing.
> Thanks
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> > When xp_cmdshell is invoked by a user who is a member of the sysadmin
> fixed
> > server role, xp_cmdshell will be executed under the security context in
> which
> > the SQL Server service is running. When the user is not a member of the
> > sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
> > account, which is specified using xp_sqlagent_proxy_account. If the proxy
> > account is not available, xp_cmdshell will fail.
> >
> > Sasan
> >
> > "Daniel Gard" wrote:
> >
> > > Sasan
> > >
> > > The application user is a SQL Server account and not a Windows user
> account.
> > > We noticed the following behavior. If the application user account was
> > > placed into the SQL System Administrator group the program runsa as the
> > > Domain account for the SQL Server and if this user is not a in the
> > > administrator group then the program runs as SQLProxy.
> > >
> > > The more I mess with this the more I think the user running the
> application
> > > after the call from SQL Server can not find the digital certificate in
> the
> > > CURRENT_USER\My store. How does the impersonation really work with SQL
> > > Server?
> > >
> > > Thanks
> > > Dan
> > >
> > >
> > > "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> > > news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> > > > Hi Daniel,
> > > >
> > > > I am just wondering under what user you did your test on query
> analyzer.
> > > >
> > > > Have you tried the following to see what kind of result you get:
> > > > 1. log in on the server by using your SQLProxy
> > > > 2. Open query analyzer and connect to the server with Windows
> > > Authentication
> > > > 3. try a test and see if the client gets the required info.
> > > >
> > > > Sasan Saidi
> > > > Senior DBA
> > > >
> > > > "Daniel Gard" wrote:
> > > >
> > > > > To any SQL Server MVP:
> > > > >
> > > > > We have a problem that out group can not seem to resolve. We have a
> > > > > database that is used to alert and calculate required amounts of
> > > assistance
> > > > > to clients that monitor through a private frame relay WAN. When a
> > > problem
> > > > > occurs client 1 enters the request at a workstation which updates
> the
> > > > > database and causes a trigger to fire and starts a process to notify
> the
> > > > > other clients that assistance is required. This portion has been in
> use
> > > for
> > > > > several years but now need to send information to a control area
> system,
> > > > > rather than single entity, that is in XML format so a program was
> > > created to
> > > > > take the data and format in accordance to the XML Schema rules the
> area
> > > > > used.
> > > > >
> > > > > At first it failed because the trigger called the EXE but the tables
> > > were
> > > > > locked by the first program called in the trigger. To resolve this
> and
> > > > > allow the trigger to continue thus unlocking the tables we changed
> the
> > > > > trigger to call a vbs script file rather than the EXE directly.
> This
> > > seemed
> > > > > to resolve the situation with locked tables in the database but a
> new
> > > issue
> > > > > has come up I can not figure out.
> > > > >
> > > > > The EXE has to use a X509 digital certificate to pass the data to
> the
> > > > > client's server. When I run the EXE from the command line it
> works,
> > > when I
> > > > > use SQL Query Manager and run the trigger code (less the alert
> portion)
> > > it
> > > > > works but when the trigger fires is fails to send the information.
> It
> > > builds
> > > > > everything but the client never gets the XML stream? We currently
> use
> > > > > simple text files to track where the program process is but this
> file
> > > > > indicates it finishes properly and all cleanup of objects occurs
> (Code
> > > in
> > > > > VB6 and we use WinHTTP Request Object 5.1). The code has been
> altered
> > > to
> > > > > indicate within the text file who started the program and this
> indicates
> > > the
> > > > > "SQLProxy" account is who is running the EXE file. It almost seems
> that
> > > when
> > > > > impersonate process occurs the local server does not treat the
> SQLProxy
> > > > > account as the right one?
> > > > >
> > > > > How can we test using SQL Query Manager the exact process as when
> the
> > > > > trigger is fired from SQL Server?
> > > > >
> > > > >
> > > > > Information:
> > > > >
> > > > > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be
> called
> > > from
> > > > > the trigger by the application user. This is what we created as
> > > instructed
> > > > > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know
> it
> > > > > works because the first step in the trigger works.
> > > > >
> > > > > CERTIFICATE: We logged on to the server using SQLProxy so the
> > > > > certificate could be installed into the "My" store for the user. We
> > > could
> > > > > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>|||Sasan,
I'll look this over maybe there is something we missed in the vbs script
being called by the trigger.
Thank You,
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:BF67C18C-AEEC-408D-9185-86D4B5DC0772@.microsoft.com...
> Daniel, I am not sure if this can help in any ways but you might want to
have
> a quick look at it:
> Article: Setting the Default Process Security Level Using VBScript
> Link:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/setting_the_default_process_security_level_using_vbscript.asp
>
> Sasan
> "Daniel Gard" wrote:
> > Sasan
> >
> > I understand that and we have all of this working. The problem seems
very
> > strange even in my book because I am not sure if the impersonation
process
> > uses a modified SID from the one if you log on the the server using the
same
> > account. Does it take the domain user and create a temporary local
account?
> > If so then that is the problem and this user can not find the
certificate.
> >
> > We are going to try to use a public certificate store and see if the
process
> > will complete.
> >
> > The program we wrote runs no matter what method the difference is when
run
> > from the command line or from SQL Query manager it finishes by sending
XML
> > to the the client but if the same process runs from being called by the
vbs
> > script that is called by the trigger the client get nothing.
> >
> > Thanks
> >
> > "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> > news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> > > When xp_cmdshell is invoked by a user who is a member of the sysadmin
> > fixed
> > > server role, xp_cmdshell will be executed under the security context
in
> > which
> > > the SQL Server service is running. When the user is not a member of
the
> > > sysadmin group, xp_cmdshell will impersonate the SQL Server Agent
proxy
> > > account, which is specified using xp_sqlagent_proxy_account. If the
proxy
> > > account is not available, xp_cmdshell will fail.
> > >
> > > Sasan
> > >
> > > "Daniel Gard" wrote:
> > >
> > > > Sasan
> > > >
> > > > The application user is a SQL Server account and not a Windows user
> > account.
> > > > We noticed the following behavior. If the application user account
was
> > > > placed into the SQL System Administrator group the program runsa as
the
> > > > Domain account for the SQL Server and if this user is not a in the
> > > > administrator group then the program runs as SQLProxy.
> > > >
> > > > The more I mess with this the more I think the user running the
> > application
> > > > after the call from SQL Server can not find the digital certificate
in
> > the
> > > > CURRENT_USER\My store. How does the impersonation really work with
SQL
> > > > Server?
> > > >
> > > > Thanks
> > > > Dan
> > > >
> > > >
> > > > "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in
message
> > > > news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> > > > > Hi Daniel,
> > > > >
> > > > > I am just wondering under what user you did your test on query
> > analyzer.
> > > > >
> > > > > Have you tried the following to see what kind of result you get:
> > > > > 1. log in on the server by using your SQLProxy
> > > > > 2. Open query analyzer and connect to the server with Windows
> > > > Authentication
> > > > > 3. try a test and see if the client gets the required info.
> > > > >
> > > > > Sasan Saidi
> > > > > Senior DBA
> > > > >
> > > > > "Daniel Gard" wrote:
> > > > >
> > > > > > To any SQL Server MVP:
> > > > > >
> > > > > > We have a problem that out group can not seem to resolve. We
have a
> > > > > > database that is used to alert and calculate required amounts of
> > > > assistance
> > > > > > to clients that monitor through a private frame relay WAN. When
a
> > > > problem
> > > > > > occurs client 1 enters the request at a workstation which
updates
> > the
> > > > > > database and causes a trigger to fire and starts a process to
notify
> > the
> > > > > > other clients that assistance is required. This portion has
been in
> > use
> > > > for
> > > > > > several years but now need to send information to a control area
> > system,
> > > > > > rather than single entity, that is in XML format so a program
was
> > > > created to
> > > > > > take the data and format in accordance to the XML Schema rules
the
> > area
> > > > > > used.
> > > > > >
> > > > > > At first it failed because the trigger called the EXE but the
tables
> > > > were
> > > > > > locked by the first program called in the trigger. To resolve
this
> > and
> > > > > > allow the trigger to continue thus unlocking the tables we
changed
> > the
> > > > > > trigger to call a vbs script file rather than the EXE directly.
> > This
> > > > seemed
> > > > > > to resolve the situation with locked tables in the database but
a
> > new
> > > > issue
> > > > > > has come up I can not figure out.
> > > > > >
> > > > > > The EXE has to use a X509 digital certificate to pass the data
to
> > the
> > > > > > client's server. When I run the EXE from the command line it
> > works,
> > > > when I
> > > > > > use SQL Query Manager and run the trigger code (less the alert
> > portion)
> > > > it
> > > > > > works but when the trigger fires is fails to send the
information.
> > It
> > > > builds
> > > > > > everything but the client never gets the XML stream? We
currently
> > use
> > > > > > simple text files to track where the program process is but this
> > file
> > > > > > indicates it finishes properly and all cleanup of objects occurs
> > (Code
> > > > in
> > > > > > VB6 and we use WinHTTP Request Object 5.1). The code has been
> > altered
> > > > to
> > > > > > indicate within the text file who started the program and this
> > indicates
> > > > the
> > > > > > "SQLProxy" account is who is running the EXE file. It almost
seems
> > that
> > > > when
> > > > > > impersonate process occurs the local server does not treat the
> > SQLProxy
> > > > > > account as the right one?
> > > > > >
> > > > > > How can we test using SQL Query Manager the exact process as
when
> > the
> > > > > > trigger is fired from SQL Server?
> > > > > >
> > > > > >
> > > > > > Information:
> > > > > >
> > > > > > ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be
> > called
> > > > from
> > > > > > the trigger by the application user. This is what we created as
> > > > instructed
> > > > > > by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we
know
> > it
> > > > > > works because the first step in the trigger works.
> > > > > >
> > > > > > CERTIFICATE: We logged on to the server using SQLProxy so
the
> > > > > > certificate could be installed into the "My" store for the user.
We
> > > > could
> > > > > > move it to HKEY_LOCAL_MACHINE\Trusted People if you think we
should.
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
> >

call to xp_cmdshell from trigger problem

To any SQL Server MVP:
We have a problem that out group can not seem to resolve. We have a
database that is used to alert and calculate required amounts of assistance
to clients that monitor through a private frame relay WAN. When a problem
occurs client 1 enters the request at a workstation which updates the
database and causes a trigger to fire and starts a process to notify the
other clients that assistance is required. This portion has been in use for
several years but now need to send information to a control area system,
rather than single entity, that is in XML format so a program was created to
take the data and format in accordance to the XML Schema rules the area
used.
At first it failed because the trigger called the EXE but the tables were
locked by the first program called in the trigger. To resolve this and
allow the trigger to continue thus unlocking the tables we changed the
trigger to call a vbs script file rather than the EXE directly. This seemed
to resolve the situation with locked tables in the database but a new issue
has come up I can not figure out.
The EXE has to use a X509 digital certificate to pass the data to the
client's server. When I run the EXE from the command line it works, when I
use SQL Query Manager and run the trigger code (less the alert portion) it
works but when the trigger fires is fails to send the information. It builds
everything but the client never gets the XML stream? We currently use
simple text files to track where the program process is but this file
indicates it finishes properly and all cleanup of objects occurs (Code in
VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
indicate within the text file who started the program and this indicates the
"SQLProxy" account is who is running the EXE file. It almost seems that when
impersonate process occurs the local server does not treat the SQLProxy
account as the right one?
How can we test using SQL Query Manager the exact process as when the
trigger is fired from SQL Server?
Information:
ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called from
the trigger by the application user. This is what we created as instructed
by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
works because the first step in the trigger works.
CERTIFICATE: We logged on to the server using SQLProxy so the
certificate could be installed into the "My" store for the user. We could
move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.Hi Daniel,
I am just wondering under what user you did your test on query analyzer.
Have you tried the following to see what kind of result you get:
1. log in on the server by using your SQLProxy
2. Open query analyzer and connect to the server with Windows Authentication
3. try a test and see if the client gets the required info.
Sasan Saidi
Senior DBA
"Daniel Gard" wrote:

> To any SQL Server MVP:
> We have a problem that out group can not seem to resolve. We have a
> database that is used to alert and calculate required amounts of assistanc
e
> to clients that monitor through a private frame relay WAN. When a problem
> occurs client 1 enters the request at a workstation which updates the
> database and causes a trigger to fire and starts a process to notify the
> other clients that assistance is required. This portion has been in use f
or
> several years but now need to send information to a control area system,
> rather than single entity, that is in XML format so a program was created
to
> take the data and format in accordance to the XML Schema rules the area
> used.
> At first it failed because the trigger called the EXE but the tables were
> locked by the first program called in the trigger. To resolve this and
> allow the trigger to continue thus unlocking the tables we changed the
> trigger to call a vbs script file rather than the EXE directly. This seem
ed
> to resolve the situation with locked tables in the database but a new issu
e
> has come up I can not figure out.
> The EXE has to use a X509 digital certificate to pass the data to the
> client's server. When I run the EXE from the command line it works, when
I
> use SQL Query Manager and run the trigger code (less the alert portion) it
> works but when the trigger fires is fails to send the information. It buil
ds
> everything but the client never gets the XML stream? We currently use
> simple text files to track where the program process is but this file
> indicates it finishes properly and all cleanup of objects occurs (Code in
> VB6 and we use WinHTTP Request Object 5.1). The code has been altered to
> indicate within the text file who started the program and this indicates t
he
> "SQLProxy" account is who is running the EXE file. It almost seems that wh
en
> impersonate process occurs the local server does not treat the SQLProxy
> account as the right one?
> How can we test using SQL Query Manager the exact process as when the
> trigger is fired from SQL Server?
>
> Information:
> ACCOUNT: SQLProxy is a domain account so xp_cmdshell can be called fro
m
> the trigger by the application user. This is what we created as instructed
> by SQLAgentCmdExec rules using xp_sqlagent_proxy_account and we know it
> works because the first step in the trigger works.
> CERTIFICATE: We logged on to the server using SQLProxy so the
> certificate could be installed into the "My" store for the user. We could
> move it to HKEY_LOCAL_MACHINE\Trusted People if you think we should.
>
>
>
>
>|||Sasan
We have tried all of the following:
1. Logged on to the Windows server as the Domain Administrator and
connecting to SQL Server using windows authentication.
2. Logged on to the Windows server as the SQLProxy Account and connecting
to SQL Server using windows authentication.
We did notice is logged on to the Windows server as either 1 or 2 and
logging onto the SQL Server as the application user it does not work.
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication[vbcol=seagreen]
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
>
assistance[vbcol=seagreen]
problem[vbcol=seagreen]
for[vbcol=seagreen]
created to[vbcol=seagreen]
were[vbcol=seagreen]
seemed[vbcol=seagreen]
issue[vbcol=seagreen]
when I[vbcol=seagreen]
it[vbcol=seagreen]
builds[vbcol=seagreen]
in[vbcol=seagreen]
to[vbcol=seagreen]
the[vbcol=seagreen]
when[vbcol=seagreen]
from[vbcol=seagreen]
instructed[vbcol=seagreen]
could[vbcol=seagreen]|||Sasan
The application user is a SQL Server account and not a Windows user account.
We noticed the following behavior. If the application user account was
placed into the SQL System Administrator group the program runsa as the
Domain account for the SQL Server and if this user is not a in the
administrator group then the program runs as SQLProxy.
The more I mess with this the more I think the user running the application
after the call from SQL Server can not find the digital certificate in the
CURRENT_USER\My store. How does the impersonation really work with SQL
Server?
Thanks
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Hi Daniel,
> I am just wondering under what user you did your test on query analyzer.
> Have you tried the following to see what kind of result you get:
> 1. log in on the server by using your SQLProxy
> 2. Open query analyzer and connect to the server with Windows
Authentication[vbcol=seagreen]
> 3. try a test and see if the client gets the required info.
> Sasan Saidi
> Senior DBA
> "Daniel Gard" wrote:
>
assistance[vbcol=seagreen]
problem[vbcol=seagreen]
for[vbcol=seagreen]
created to[vbcol=seagreen]
were[vbcol=seagreen]
seemed[vbcol=seagreen]
issue[vbcol=seagreen]
when I[vbcol=seagreen]
it[vbcol=seagreen]
builds[vbcol=seagreen]
in[vbcol=seagreen]
to[vbcol=seagreen]
the[vbcol=seagreen]
when[vbcol=seagreen]
from[vbcol=seagreen]
instructed[vbcol=seagreen]
could[vbcol=seagreen]|||When xp_cmdshell is invoked by a user who is a member of the sysadmin fixed
server role, xp_cmdshell will be executed under the security context in whic
h
the SQL Server service is running. When the user is not a member of the
sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
account, which is specified using xp_sqlagent_proxy_account. If the proxy
account is not available, xp_cmdshell will fail.
Sasan
"Daniel Gard" wrote:

> Sasan
> The application user is a SQL Server account and not a Windows user accoun
t.
> We noticed the following behavior. If the application user account was
> placed into the SQL System Administrator group the program runsa as the
> Domain account for the SQL Server and if this user is not a in the
> administrator group then the program runs as SQLProxy.
> The more I mess with this the more I think the user running the applicatio
n
> after the call from SQL Server can not find the digital certificate in the
> CURRENT_USER\My store. How does the impersonation really work with SQL
> Server?
> Thanks
> Dan
>
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:A3A3E257-2889-4C54-BB23-05B4B352F696@.microsoft.com...
> Authentication
> assistance
> problem
> for
> created to
> were
> seemed
> issue
> when I
> it
> builds
> in
> to
> the
> when
> from
> instructed
> could
>
>|||Sasan
I understand that and we have all of this working. The problem seems very
strange even in my book because I am not sure if the impersonation process
uses a modified SID from the one if you log on the the server using the same
account. Does it take the domain user and create a temporary local account?
If so then that is the problem and this user can not find the certificate.
We are going to try to use a public certificate store and see if the process
will complete.
The program we wrote runs no matter what method the difference is when run
from the command line or from SQL Query manager it finishes by sending XML
to the the client but if the same process runs from being called by the vbs
script that is called by the trigger the client get nothing.
Thanks
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> When xp_cmdshell is invoked by a user who is a member of the sysadmin
fixed
> server role, xp_cmdshell will be executed under the security context in
which[vbcol=seagreen]
> the SQL Server service is running. When the user is not a member of the
> sysadmin group, xp_cmdshell will impersonate the SQL Server Agent proxy
> account, which is specified using xp_sqlagent_proxy_account. If the proxy
> account is not available, xp_cmdshell will fail.
> Sasan
> "Daniel Gard" wrote:
>
account.[vbcol=seagreen]
application[vbcol=seagreen]
the[vbcol=seagreen]
analyzer.[vbcol=seagreen]
the[vbcol=seagreen]
the[vbcol=seagreen]
use[vbcol=seagreen]
system,[vbcol=seagreen]
area[vbcol=seagreen]
and[vbcol=seagreen]
the[vbcol=seagreen]
This[vbcol=seagreen]
new[vbcol=seagreen]
the[vbcol=seagreen]
works,[vbcol=seagreen]
portion)[vbcol=seagreen]
It[vbcol=seagreen]
use[vbcol=seagreen]
file[vbcol=seagreen]
(Code[vbcol=seagreen]
altered[vbcol=seagreen]
indicates[vbcol=seagreen]
that[vbcol=seagreen]
SQLProxy[vbcol=seagreen]
the[vbcol=seagreen]
called[vbcol=seagreen]
it[vbcol=seagreen]|||Daniel, I am not sure if this can help in any ways but you might want to hav
e
a quick look at it:
Article: Setting the Default Process Security Level Using VBScript
Link:
http://msdn.microsoft.com/library/d...ng_vbscript.asp
Sasan
"Daniel Gard" wrote:

> Sasan
> I understand that and we have all of this working. The problem seems very
> strange even in my book because I am not sure if the impersonation process
> uses a modified SID from the one if you log on the the server using the sa
me
> account. Does it take the domain user and create a temporary local account
?
> If so then that is the problem and this user can not find the certificate.
> We are going to try to use a public certificate store and see if the proce
ss
> will complete.
> The program we wrote runs no matter what method the difference is when run
> from the command line or from SQL Query manager it finishes by sending XML
> to the the client but if the same process runs from being called by the vb
s
> script that is called by the trigger the client get nothing.
> Thanks
> "Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
> news:7C116F51-C3BB-4ADD-96FC-9231D90287BE@.microsoft.com...
> fixed
> which
> account.
> application
> the
> analyzer.
> the
> the
> use
> system,
> area
> and
> the
> This
> new
> the
> works,
> portion)
> It
> use
> file
> (Code
> altered
> indicates
> that
> SQLProxy
> the
> called
> it
>
>|||Sasan,
I'll look this over maybe there is something we missed in the vbs script
being called by the trigger.
Thank You,
Dan
"Sasan Saidi" <SasanSaidi@.discussions.microsoft.com> wrote in message
news:BF67C18C-AEEC-408D-9185-86D4B5DC0772@.microsoft.com...
> Daniel, I am not sure if this can help in any ways but you might want to
have
> a quick look at it:
> Article: Setting the Default Process Security Level Using VBScript
> Link:
>
http://msdn.microsoft.com/library/d...ng_vbscript.asp[vbcol=seagreen]
>
> Sasan
> "Daniel Gard" wrote:
>
very[vbcol=seagreen]
process[vbcol=seagreen]
same[vbcol=seagreen]
account?[vbcol=seagreen]
certificate.[vbcol=seagreen]
process[vbcol=seagreen]
run[vbcol=seagreen]
XML[vbcol=seagreen]
vbs[vbcol=seagreen]
in[vbcol=seagreen]
the[vbcol=seagreen]
proxy[vbcol=seagreen]
proxy[vbcol=seagreen]
was[vbcol=seagreen]
the[vbcol=seagreen]
in[vbcol=seagreen]
SQL[vbcol=seagreen]
message[vbcol=seagreen]
have a[vbcol=seagreen]
a[vbcol=seagreen]
updates[vbcol=seagreen]
notify[vbcol=seagreen]
been in[vbcol=seagreen]
was[vbcol=seagreen]
the[vbcol=seagreen]
tables[vbcol=seagreen]
this[vbcol=seagreen]
changed[vbcol=seagreen]
a[vbcol=seagreen]
to[vbcol=seagreen]
information.[vbcol=seagreen]
currently[vbcol=seagreen]
seems[vbcol=seagreen]
when[vbcol=seagreen]
know[vbcol=seagreen]
the[vbcol=seagreen]
We[vbcol=seagreen]
should.[vbcol=seagreen]

Wednesday, March 7, 2012

Call external program from a stored proc / trigger

What is the stored proc that can call an external exe, etc. from a stored
procedure, function, trigger, etc.?
I can not for the life of me remember what it is. I need to an action upon
row inserts and wanted to have a trigger run an external program.
thanks,master..xp_cmdshell
AMB
"aiKeith" wrote:

> What is the stored proc that can call an external exe, etc. from a stored
> procedure, function, trigger, etc.?
> I can not for the life of me remember what it is. I need to an action upo
n
> row inserts and wanted to have a trigger run an external program.
> thanks,
>|||Hi
You can call xp_cmdshell, but it is ill advised.
What happens if your trigger fires again before the application finishes?
If the external application fails, the error could rollback the DML done.
The locks aquired by the DML, remain in force until the SP or Trigger
completes. This can cuase concurrency problems.
Regards
Mike
"aiKeith" wrote:

> What is the stored proc that can call an external exe, etc. from a stored
> procedure, function, trigger, etc.?
> I can not for the life of me remember what it is. I need to an action upo
n
> row inserts and wanted to have a trigger run an external program.
> thanks,
>|||Thank you Alej...
I believe there is a specific procedure for calling external applications /
dll's, etc. I even had a sample that called a C++ applications dll, but i
cannot find it. I did not want to use the cmd shell.
Any ideas?
"Alejandro Mesa" wrote:
> master..xp_cmdshell
>
> AMB
> "aiKeith" wrote:
>|||Hi
It is considered a bad idea to run .EXE from a trigger. Look , when a
trigger gets fired it opens a new transaction and keeps it open till all
commands will be completed succesfuly. If SQL Server raises an error inside
the trigger all transactions should be rollbacked which may cause to
locks,blocking or even deadlocks. Do you really ,really need it to be ran
from the trigger?
"aiKeith" <aiKeith@.discussions.microsoft.com> wrote in message
news:A067E30C-E88E-40D2-AD5D-181FFB98B664@.microsoft.com...
> What is the stored proc that can call an external exe, etc. from a stored
> procedure, function, trigger, etc.?
> I can not for the life of me remember what it is. I need to an action
upon
> row inserts and wanted to have a trigger run an external program.
> thanks,
>|||sp_OA*
AMB
"aiKeith" wrote:
> Thank you Alej...
> I believe there is a specific procedure for calling external applications
/
> dll's, etc. I even had a sample that called a C++ applications dll, but i
> cannot find it. I did not want to use the cmd shell.
> Any ideas?
> "Alejandro Mesa" wrote:
>|||You probably want the sp_OA procs. Lookup sp_oacreate in Books Online.
As Mike indicated, this is a really bad idea in a trigger. One issue is
how to handle a transaction that rolls back. Since there is no built-in
functionality for that you would have to detect any error condition and
call your external process again to rollback its action. You therefore
gain little from doing it in a trigger (why not just call your code
once only, at the end).
On the other hand if you don't need this to be part of an atomic
transaction then there is no need to put it in a trigger at all. Best
bet is to create an external process, outside of SQL Server, that
regularly polls the table for changes.
David Portas
SQL Server MVP
--|||Thank you all for your great comments.
I am aware that calling code from a trigger is typically bad practice, and I
almost included that in the first post.
This is only for a temporary thing... the reason I want to do it in a
trigger is:
1.) Less resources... I dont know if its going to run today, next w or
not for a month, therefore I dont want something constantly monitoring the d
b
(even if its once a day)
2.) I can easily disable/remove the trigger.
Thanks again, you guys inspire me to answer posts.
"Uri Dimant" wrote:

> Hi
> It is considered a bad idea to run .EXE from a trigger. Look , when a
> trigger gets fired it opens a new transaction and keeps it open till all
> commands will be completed succesfuly. If SQL Server raises an error insid
e
> the trigger all transactions should be rollbacked which may cause to
> locks,blocking or even deadlocks. Do you really ,really need it to be ran
> from the trigger?
>
>
> "aiKeith" <aiKeith@.discussions.microsoft.com> wrote in message
> news:A067E30C-E88E-40D2-AD5D-181FFB98B664@.microsoft.com...
> upon
>
>

Saturday, February 25, 2012

Call a DLL or EXE file from SQL Trigger

I need some help calling a DLL or EXE from a SQL Trigger. I have the trigger set up, except I have no clue how to call a DLL or EXE or if it is even possible. Here is what I have for the Trigger so far:


-- ================================================

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

CREATE TRIGGER CallProgIfParentID

ON Toc

FOR INSERT

AS

IF ((select ins.[ParentID] FROM inserted ins) = '57660')

-- I want to be able to programmatically change '57660' from an ASPX page

EXEC

-- This is where the call function goes

GO


Another question I have is can a trigger be created programmatically using ASPX pages written in VB.NET in VS2003?

Thank you in advance

I have to warn you first: although calling external exes is possible, it is not recommended as there are at least a couple obvious drawbacks:

1. You would possibly lose data integrity because those external processes are NOT bound to the SQL transaction that triggers run in. For example, if the INSERT statement that fires the insert trigger is rolled back, regular trigger actions would be rolled back too, but those external processes would not. Similarly, triggers might not be able to detect errors that happen to those external processes and it ends up with the external exe failed but the trigger (as well as the firing statement) succeeded.
2. These processes will run outside of SQL Server, so you would lose total control of them. E.g. they might come back and compete with SQL Server for resources like CPU and memory.

I am wondering what kind of scenarios you have, but there got to be a better way to do it :)

Anyway, if you still decide to go with this route, you could call external exes from a trigger by:

- xp_cmdshell '<some exe>'
- For a *unsafe* CLR trigger, you could practically do anything, including calling external processes (e.g. Process class)

For you other question, yes, you can create triggers programmatically from wherever you can connect to the server.

|||

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

GO

CREATE TRIGGER SendMailIfParentID

ON Toc

AFTER INSERT

AS

IF ((select ins.[ParentID] FROM inserted ins) = '57660')

EXEC master ..xp_cmdshell 'C:\Documents and Settings\michael\My Documents\Visual Studio Projects\Test\bin\Test.exe 57660'


Thats what I have for my trigger now (The 57660 is a test ID number and isn't of much relavence), either the trigger isnt being called or the EXE isn't being called. I can't figure out why it's not working.

|||

I figured out what wasn't working and fixed it. Now I can't get a variable to append to the end of the command line. It keeps telling me the + isn't valid. Does anyone have any ideas?


ALTER TRIGGER SendMailIfParentID

ON Toc

AFTER INSERT

AS

IF ((select ins.[ParentID] FROM inserted ins) = '57750')

DECLARE @.MyTocId varchar(12)

SELECT @.MyTocId = (SELECT TocId FROM inserted)

EXEC master ..xp_cmdshell '"C:\Documents and Settings\michael\My Documents\Visual Studio Projects\Test\bin\Test.exe" ' + @.MyTocId

GO

|||Hi,

for debugging and better handling purposes, I would suggest first putting evverything in a varaible and executing this afterwards:

IF ((select ins.[ParentID] FROM inserted ins) = '57750')

DECLARE @.MyTocId varchar(12)

SELECT @.MyTocId = '"C:\Documents and Settings\michael\My Documents\Visual Studio Projects\Test\bin\Test.exe" ' + TocId FROM inserted

EXEC master ..xp_cmdshell @.command = @.MyTocId

Warning: Triggers are fired per statement not per row, you will in addition make sure that your trigger is able to handle multiple rows affected in a trigger. In further addition you will have to make sure that the command is not executed as no row is affected as the trigger is fired (as already said) on a statement basis (even if the affected rowcount is 0).


Jens K. Suessmeyer.

http://www.sqlserver2005.de