Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:48:32+00:00 2026-06-11T22:48:32+00:00

I have an insert trigger that contains dynamic sql stored as a resource for

  • 0

I have an insert trigger that contains dynamic sql stored as a resource for my XE2 project. It also contains placeholders for the database name and table name that are substituted when the Delph code runs to execute the SQL.

Originally I was using the DevArt SQL Server driver against a SqlExpress database, but am now wanting to use ODBC and the SQL Native Client driver against a LocalDB database.

What I have found is that my original create trigger scripts no longer work.

I am using TSQLQuery.ExecSQL to execute my SQL commands.

CREATE TRIGGER [dbo].[#TableName#_INSERT_TRIGGER] ON [#DatabaseName#].[dbo].[#TableName#] FOR INSERT

causes a

‘[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot create trigger on ‘EvaluationCompany_COPYDB.dbo.COPY_PRODUCTS’ as the target is not in the current database.’

The parser class I use does split SQL scripts at the GO keyword into separate statements, so I amended my create trigger script to say

USE [#DatabaseName#]
GO
CREATE TRIGGER [dbo].[#TableName#_INSERT_TRIGGER] ON [dbo].[#TableName#] FOR INSERT

which is what you would do in SSMS but that says

‘[Microsoft][SQL Server Native Client 11.0][SQL Server]The object ‘dbo.COPY_PRODUCTS’ does not exist or is invalid for this operation.’

Probably because the “current database” for the CREATE is not the one set by USE, as it seems to be forgotten.

I tried my usual way of stringing sql statements into a single execute by doing

USE [#DatabaseName#];
CREATE TRIGGER [dbo].[#TableName#_INSERT_TRIGGER] ON [dbo].[#TableName#] FOR INSERT

but that throws the expected

‘[Microsoft][SQL Server Native Client 11.0][SQL Server]’CREATE TRIGGER’ must be the first statement in a query batch.’

So I wrapped the whole CREATE…END in a EXEC [#DatabaseName#].[sys].[sp_ExecuteSQL] N’ ‘ and tried to execute that. If I paste the contents of the string variable into SSMS it executes fine, but when passed to the ExecSQL, it says

‘[Microsoft][SQL Server Native Client 11.0][SQL Server]The request for procedure ‘sp_executesql’ failed because ‘sp_executesql’ is a procedure object.’

which is kind of nonsensical. So now I am at a loss as to how to create a trigger on a table using dbExpress and the SQL Server native client.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:48:33+00:00Added an answer on June 11, 2026 at 10:48 pm

    I’m not sure exactly what the problem is, but I was able to successfully run the following code.
    Maybe it will give you a clue.

    uses
      Data.Win.AdoDB;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      aConnection : tAdoConnection;
    
      procedure InitializeAdoConnection;
      begin
        aConnection := tAdoConnection . Create ( self );
    
        with aConnection do
          begin
            ConnectionString :=   'Provider=MSDASQL.1;'
                                + 'Password=' + gPassword + ';'
                                + 'Persist Security Info=True;'
                                + 'User ID=' + gUserName + ';'
                                + 'Data Source=' + gOdbcAlias + ';'
                                + 'Extended Properties="DSN=' + gOdbcAlias + ';'
                                + 'UID=' + gUserName + ';'
                                + 'PWD=' + gPassword + ';'
                                + 'APP=Enterprise;'
                                + 'WSID=' + gMachineName + ';'
                                + 'DATABASE=master";'
                                + 'Initial Catalog=master';
            LoginPrompt := false;
            Connected := true;
          end;
      end;
    
      procedure ExecuteCommand ( const nSqlCommand : string );
      begin
        with tAdoCommand . Create ( nil ) do
          try
            Connection := aConnection;
            CommandText := nSqlCommand;
            Execute;
          finally
            Free;
          end;
      end;
    
      procedure QueryResults;
      begin
        with tAdoQuery . Create ( nil ) do
          try
            Connection := aConnection;
            SQL . Text := 'select * from COPY_PRODUCTS';
            Open;
    
            while not EOF do
              begin
                Memo1 . Lines . Add ( 'ID='
                      + inttostr ( FieldByName ( 'PRODUCT_ID' ) . AsInteger )
                      + ' Name='
                      + FieldByName ( 'PRODUCT_NAME' ) . AsString );
    
                Next;
              end;
    
    
          finally
            Free;
          end;
      end;
    
    begin
      InitializeAdoConnection;
    
    //  ExecuteCommand ( 'drop database EvaluationCompany_COPYDB' );
    
      ExecuteCommand ( 'create database EvaluationCompany_COPYDB' );
    
      ExecuteCommand ( 'use EvaluationCompany_COPYDB' );
    
      ExecuteCommand ( 'create table dbo.COPY_PRODUCTS '
                       + '( PRODUCT_ID int identity(1,1),'
                       + '  PRODUCT_NAME varchar(50) )' );
    
      ExecuteCommand ( 'create trigger dbo.COPY_PRODUCTS_INSERT_TRIGGER '
                       + 'on dbo.COPY_PRODUCTS '
                       + 'for insert '
                       + 'as '
                       + 'begin '
                       + '  update COPY_PRODUCTS '
                       + '    set PRODUCT_NAME = PRODUCT_NAME + ''!'' '
                       + '    where PRODUCT_ID in '
                       + '    ( select PRODUCT_ID from INSERTED )'
                       + 'end ' );
    
      ExecuteCommand ( 'insert into COPY_PRODUCTS ( product_name ) '
                       + 'values ( ''Stacky Goodness'' ) ' );
    
      QueryResults;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trigger for insert/update/delete. That is working fine. Also, I need the
I have a view that has an INSTEAD OF INSERT trigger (in SQL Server
I have a trigger that check data before insert to another table IF NOT
I have a stored procedure which is called inside a trigger on Insert/Update/Delete. The
I have a function that is used as an INSERT trigger. This function deletes
I have some stored procedures and a trigger that work great in MySQL 5.5.8
I have an AFTER INSERT OR UPDATE OR DELETE trigger that I'm writing to
I have a table that records positions (gps) and fires insert trigger, where I
I have an article table on a Postgresql 9.1 database and a trigger that
I have MS SQL Server database and insert some values to one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.