sql like variable 1

sql like variable

ALTER PROCEDURE 
(
    @PartialName VARCHAR(50) = NULL
)

SELECT Name 
    FROM 
    WHERE Name LIKE '%' + LTRIM(RTRIM(@PartialName)) + '%'

Here is what the above code is Doing:
1. The first line is the name of the stored procedure. You can name it whatever you want.
2. The next line is the parameter list. You can have as many parameters as you want. In this case, I have one parameter called @PartialName. The VARCHAR(50) is the data type of the parameter. The = NULL means that the parameter is optional. If you don’t pass a value for the parameter, it will be NULL.
3. The next line is the SELECT statement. This is the code that will be executed when you call the stored procedure. In this case, it’s a simple SELECT statement that returns all rows from the table where the Name column contains the value of the @PartialName parameter.
4. The last line is the end of the stored procedure.

To call the stored procedure, you would use the following code:

EXEC ‘John’

The above code will return all rows where the Name column contains the value ‘John’. If you want to return all rows, you can call the stored procedure without passing a value for the @PartialName parameter:

EXEC

The above code will return all rows from the table.

If you want to return all rows where the Name column contains the value ‘John’ and the Age column contains the value ’25’, you can create a stored procedure like this:

Similar Posts