4

Can someone help me with the syntax attached

DECLARE @SearchString NVARCHAR(MAX)
SET     @SearchString = 'fletc'

USE [Prodution]
GO

SELECT * FROM [User] WHERE Username LIKE '%' + @SearchString + '%'

I am trying to concatenate the LIKE statement at the bottom. However, I am getting the error message attached:

Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@SearchString".

Any help would be greatly appreciated.

thanks

Rob

2 Answers 2

7

The use of GO terminates a scope (so all variables declared before it are "lost") - move your declaration to after the GO:

USE [Production]
GO

DECLARE @SearchString NVARCHAR(MAX)
SET     @SearchString = 'fletc'

SELECT * FROM [User] WHERE Username LIKE '%' + @SearchString + '%'
Sign up to request clarification or add additional context in comments.

1 Comment

Hello Marc, many thanks for your help. It works perfectly now.
1

In your code:

DECLARE @SearchString NVARCHAR(MAX)
SET     @SearchString = 'fletc'

USE [Prodution]
GO

SELECT * FROM [User] WHERE Username LIKE '%' + @SearchString + '%'

there's a GO statement. Visibility starts again after GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.