Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Yet another SQL Question...


ghost's Avatar
0 0

i have a clr stored procedure written in c# that starts a thread to call a certain command asynchronously. the command is given as a parameter like so: markup exec sp_processor [insert command]

now this works perfectly when run manually. smooth and accomplishes the asynchronous behavior i want. however when i put it into my trigger code it never runs although the trigger fires perfectly.

here's the trigger code

ALTER TRIGGER [dbo].[trig_newRow] ON [dbo].[TestTable]
AFTER INSERT
AS
DECLARE @row int
DECLARE @cmd varchar(50)
SET @row = (SELECT TestIndex FROM INSERTED)
SET @cmd = 'exec sp_initiateJob '+CONVERT(VARCHAR(20), @row)

PRINT 'Command:'+@cmd
PRINT 'Trigger fired...'
EXEC sp_processor [@cmd]
PRINT 'sp_processor shoulda fired...```

the print statements are for debugging and all the print statements are called fine.. also i put in other stored procedures to execute even another written in c# and those run perfectly.

as long as i'm here i might as well post the c# code so you can tell me if there is a problem with that. here it is:
```markup
using System;
using System.Threading;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

public class FileTrigger
{
    private static SqlConnection cnn;
    private static string command;

    [SqlProcedure]
    public static void processRow(string cmd)
    {
            try
            {
                cnn = new SqlConnection(getConnectionString());
                cnn.Open();
                command = cmd;
                Thread processor = new Thread(process);
                processor.Start();
            }
            catch
            {
                getConnectionString();
            }
    }

    private static void process()
    {
        SqlCommand cmd = new SqlCommand(command, cnn);
        cmd.ExecuteNonQuery();
        cnn.Close();
    }

    private static string getConnectionString()
    {
        return "Data Source=xxx.xxx.x.xx;Initial Catalog=xxxxxxx; User ID=xxxxxxx; Password=xxxx";
    }
}

so why will this run manually like a charm but right when put into the trigger it doesn't run.. Thanks in advance.

Jake


ghost's Avatar
0 0

have you checked the permissions?