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 6159729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:10:51+00:00 2026-05-23T21:10:51+00:00

Summary I’m currently writing an application where I have located my SQL instructions into

  • 0

Summary

I’m currently writing an application where I have located my SQL instructions into a project’s parameters.

Within the code, I get the value of my query which returns the query itself. Let’s for instance say that my SQL query is like so:

select col1, col2, col3 from my_table

Besides, col1, col2 and col3 are from different tables and are migrated as foreign key into my_table. So, when it comes to the insert, I have to perform multiple INSERT statements to get the values from the other tables for these above-mentioned columns. Let’s say as follows:

BEGIN TRANSACTION

insert into first_table (col_x, col_y) values ('col_x', 'col_y')
insert into second_table (col_z, col_a) values ('col_z', 'col_a')
insert into third_table (col_b, col_c) values ('col_b', 'col_c')

and finally:

insert into my_table (col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v')

COMMIT

Take it that these col1, col2, col3 columns are auto-increment integers for tables first, second and third.

Questions

  1. Could I write a complex SQL statement into the IDbCommand.CommandText property while each instruction would be separated by a semicolon (;)?

  2. Is it possible to include a BEGIN TRANSACTION...COMMIT/ROLLBACK into this CommandText property?

  3. In short, could I write something like this?

    Using cnx = New SqlConnection(connString)
        Using cmd = cnx.CreateCommand()
            cmd.CommandText = "BEGIN TRANSACTION " _
                      & "insert into first_table (col_x, col_y) values ('col_x', 'col_y');" _ 
                      & "insert into second_table (col_z, col_a) values ('col_z', 'col_a');" _
                      & "insert into third_table (col_b, col_c) values ('col_b', 'col_c');" _
                      & "insert into my_table (col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v'); " _
                      & "COMMIT"
            cmd.ExecuterNonQuery()
        End Using
    End Using
    

EDIT #1

I should have mentioned it before… Mack’s answer is the way I would like to go, except that I can’t because of strange policies within the IT department of my client, except if I use their custom component which I rather avoid for simplicity sake. Notice that I upvoted Mack’s answer anyway since it is a viable solution no matter what.

Thanks in advance for your precious help and time! This is crucial for me!

  • 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-05-23T21:10:52+00:00Added an answer on May 23, 2026 at 9:10 pm

    If you can’t use stored procedures then perhaps this code may meet your requirements:

    SqlConnection cnx = new SqlConnection(connString);
    SqlCommand cmd = cnx.CreateCommand();
    cnx.Open();
    string complexCommand = string.Concat(
    "DECLARE @first_table AS TABLE(col1 int IDENTITY, col_x varchar(20), col_y varchar(20))"
    , " DECLARE @second_table AS TABLE(col2 int IDENTITY, col_z varchar(20), col_a varchar(20))"
    , " DECLARE @third_table AS TABLE(col3 int IDENTITY, col_b varchar(20), col_c varchar(20))"
    , " DECLARE @my_table AS TABLE(col1 int, col2 int, col3 int, col_v varchar(20))"
    , " DECLARE @col1 int"
    , " DECLARE @col2 int"
    , " DECLARE @col3 int"
    , " BEGIN TRAN"
    , " BEGIN TRY"
    , "   insert into @first_table (col_x, col_y) values ('col_x', 'col_y')"
    , "   SET @col1=@@IDENTITY"
    , "   insert into @second_table (col_z, col_a) values ('col_z', 'col_a')"
    , "   SET @col2=@@IDENTITY"
    , "   insert into @third_table (col_b, col_c) values ('col_b', 'col_c')"
    , "   SET @col3=@@IDENTITY"
    , "   insert into @my_table(col1, col2, col3, col_v) values (@col1, @col2, @col3, 'col_v')"
    , "   COMMIT"
    , " END TRY"
    , " BEGIN CATCH"
    , "   ROLLBACK"
    , " END CATCH");
    
    cmd.CommandText = complexCommand;
    cmd.ExecuteNonReader();
    

    I have added table variables as necessary to get the example code running, obviously you can utilise your permanent tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary We have an ASP.NET application that allows users to query a SQL Server
Summary I currently have a NAnt build script that performs a vssget on either
Summary: Say I have a project in Django called devsite which will be deployed
Summary I have a web-application with more than 5 themes. Each themes covers a
Summary : I have a table populated via the following: insert into the_table (...)
Summary Hi All, OK, further into my adventures with custom controls... In summary, here
Summary: I'm developing a persistent Java web application, and I need to make sure
Summary: I'm able to compile a RAD Studio 2009 project using MSBuild on a
I need to have a summary field in each page of the report and
Is there an easy way to get a conflict summary after running a cvs

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.