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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:01:35+00:00 2026-06-15T14:01:35+00:00

I’m trying to write a script in T-SQL to resolve a critical issue on

  • 0

I’m trying to write a script in T-SQL to resolve a critical issue on our system. Unfortunately I have never done this before so I’m entering uncharted waters.

I’m actually writing a PHP script to generate this SQL. If the user exists, create it. But in both cases, add the user to a group.

DECLARE @userId AS int

/* --------------- 123l123123fvsdf - Name --------------- */
    BEGIN
        IF EXISTS(SELECT id FROM [CMT_UAT].[dbo].[user] WHERE username = '123l123123fvsdf')
                INSERT INTO [CMT_UAT].[dbo].[user](username, password, email, firstname, lastname, created) VALUES('123l123123fvsdf', 'PASSWORD', 'aa00043@user.com', 'firstName', 'lastName', GETDATE())
                GO
                SET @userId = @@IDENTITY
        ELSE
                SET @userId = SELECT id FROM [CMT_UAT].[dbo].[user] WHERE username = 'AA00043'
    END
    INSERT INTO [CMT_UAT].[dbo].[user_group](user_id, group_id) VALUES(@userId, '7')

I’m getting these errors:

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ‘)’.
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable “@userId”.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword ‘SELECT’.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword ‘INSERT’.
Msg 137, Level 15, State 2, Line 5
Must declare the scalar variable “@userId”.

I’ve done some googling on the scalar value and it can happen when my query returns multiple rows, which I’ve verified that it doesn’t.

  • 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-15T14:01:36+00:00Added an answer on June 15, 2026 at 2:01 pm

    you need to fix your if statement. try:

    DECLARE @userId AS int
    
    IF EXISTS(SELECT id FROM [CMT_UAT].[dbo].[user] WHERE username = '123l123123fvsdf')
    BEGIN
        INSERT INTO [CMT_UAT].[dbo].[user](username, password, email, firstname, lastname, created) VALUES('123l123123fvsdf', 'PASSWORD', 'aa00043@user.com', 'firstName', 'lastName', GETDATE())
        SET @userId = @@IDENTITY
    END
    ELSE
       SET @userId = SELECT id FROM [CMT_UAT].[dbo].[user] WHERE username = 'AA00043'
    
    INSERT INTO [CMT_UAT].[dbo].[user_group](user_id, group_id) VALUES(@userId, '7')
    

    you have multipl statements in the first part of your if block but you forgot to put BEGIN and END around it

    And also like RBarryYoung pointed out you need to take the GO out

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put

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.