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

  • SEARCH
  • Home
  • 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 6622045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:21:11+00:00 2026-05-25T21:21:11+00:00

I have an insert statement that I can’t get to work the way I

  • 0

I have an insert statement that I can’t get to work the way I want it to. It’s on a vb.net page. This is on a VB.net page and I’m using SQL Server 2005 for my database.

Dim strSQL As String = "IF NOT EXISTS
(SELECT Title From Picklist) 
BEGIN INSERT INTO Picklist (Title, Data) 
VALUES (@Title, @Data);

INSERT INTO Marketing 
(ProductID, MarketingTypeID, MarketingTitle, MarketingData) 
VALUES (@ProductID, 9, 'Video', scope_identity()) END"

I don’t get an error and nothing gets inserted into the database. If I try putting the END at the end of the first INSERT statement then I get an error saying that MarketingData is NULL and cannot be inserted.

But if I take out the IF NOT EXISTS from the statement, everything gets inserted perfectly. What am I doing wrong here?

UPDATE: Is it correct to write the statement like this?

INSERT INTO Marketing
SELECT (@ProductID, @MarketingTypeID, @MarketingTitle, @MarketingData)
WHERE NOT EXISTS
(SELECT * FROM Marketing)
  • 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-25T21:21:12+00:00Added an answer on May 25, 2026 at 9:21 pm

    Your IF NOT EXISTS(SELECT * FROM Picklist) will skip the insert if any rows at all exist in Picklist.

    From your description of what happens when you change the position of the END it seems there are rows in the table.

    I assume in fact you are trying to do an UPSERT. What version of SQL Server are you on? If 2008 look into MERGE

    ;WITH Source(Title, Data) AS
    (
    SELECT @Title, @Data
    )
    MERGE Picklist AS T
    USING Source S
    ON (T.Title = S.Title)
    
    
    WHEN NOT MATCHED BY TARGET THEN
        INSERT (Title, Data)
        VALUES (@Title, @Data)
        ;
    
    IF (@@ROWCOUNT <> 0)
    INSERT INTO Marketing 
                (ProductID, MarketingTypeID, MarketingTitle, MarketingData) 
         VALUES (@ProductID, 9, 'Video', scope_identity())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql insert statement that breaks when the system can't get lat/lng
I have an sql statement that I posted on this site, and I applied
I have an INSERT statement that looks like this: INSERT INTO officer (officer_number, name,
I have an insert statement that was deadlocking using linq. So I placed it
HI, I have an insert statement in a Stored Procedure that requires the value
I have one multi-row INSERT statement (300 or so sets of values) that I
I have a SQL script that inserts data (via INSERT statements currently numbering in
SQL statement: INSERT INTO order (`ORDER_ID`,`SALE_CODE`,`CREATED_AT`,`UPDATED_AT`) VALUES ('2646253286','HPHS20','2009-07-11 12:07:40','2009-07-11 12:07:40') Error: You have an
This is a really odd situation that I can't seem to work out where
I have a MySQL insert statement that inserts data from an existing Despgoods table

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.