elena1234

Dynamic SQL ( How to concatenate varchar and variable)

Mar 28th, 2022 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.29 KB | None | 0 0
  1. USE [AdventureWorks2019]
  2. GO
  3.  
  4.  
  5. /* Exercise 1
  6. Create a stored procedure called "NameSearch" that allows users to search the Person.Person table for a pattern provided by the user.
  7. The user should be able to search by either first name, last name, or middle name.
  8. You can return all columns from the table; that is to say, feel free to user SELECT *.
  9. The stored procedure should take two arguments:
  10. @NameToSearch: The user will be expected to enter either "first", "middle", or "last". This way, they do not have to remember exact column names.
  11. @SearchPattern: The user will provide a text string to search for. */
  12.  
  13. CREATE or ALTER PROCEDURE dbo.NameSearch(@NameToSearch VARCHAR(100), @SearchPattern VARCHAR(100))
  14. AS
  15. BEGIN
  16.         DECLARE @DynamicVariable VARCHAR(MAX)
  17.         SET @DynamicVariable = 'SELECT * FROM Person.Person
  18.         where '
  19.         SET @DynamicVariable = @DynamicVariable + ' ' + @NameToSearch + 'Name'
  20.         SET @DynamicVariable = @DynamicVariable + ' LIKE ' + '''' + '%' +  @SearchPattern + '%' + ''''
  21.         EXEC(@DynamicVariable)
  22. END
  23.  
  24. dbo.NameSearch 'FIRST', 'KEN'
  25. dbo.NameSearch 'MIDDLE', 'T'
  26. dbo.NameSearch 'LAST', 'SHE'
  27.  
  28.  
  29. /* Exercise 2
  30. Modify your "NameSearch" procedure to accept a third argument - @MatchType, with an INT datatype -  that specifies the match type:
  31.     1 means "exact match"
  32.     2 means "begins with"
  33.     3 means "ends with"
  34.     4 means "contains" */
  35.  
  36. CREATE or ALTER PROCEDURE dbo.NameSearch(@NameToSearch VARCHAR(100), @SearchPattern VARCHAR(100), @MatchType INT)
  37. AS
  38. BEGIN
  39.  
  40.     DECLARE @DynamicVariable VARCHAR(MAX)
  41.     SET @DynamicVariable = 'SELECT * FROM Person.Person
  42.     where '
  43.     SET @DynamicVariable = @DynamicVariable + ' ' + @NameToSearch + 'Name'
  44.             IF  @MatchType = 1
  45.                 SET @DynamicVariable = @DynamicVariable + ' LIKE ' + '''' + @SearchPattern + ''''
  46.             ELSE IF @MatchType = 2
  47.                 SET @DynamicVariable = @DynamicVariable + ' LIKE ' + '''' + @SearchPattern + '%' + ''''
  48.             ELSE IF @MatchType = 3
  49.                 SET @DynamicVariable = @DynamicVariable + ' LIKE ' + '''' + '%' + @SearchPattern + ''''
  50.             ELSE IF @MatchType = 4
  51.                 SET @DynamicVariable = @DynamicVariable + ' LIKE ' + '''' + '%' +  @SearchPattern + '%' + ''''
  52.     EXEC(@DynamicVariable)
  53. END
  54.  
  55. dbo.NameSearch 'FIRST', 'KEN', 1
  56. dbo.NameSearch 'MIDDLE', 'T', 2
  57. dbo.NameSearch 'LAST', 'HE', 3
  58. dbo.NameSearch 'FIRST', 'KEN', 4
  59.  
Add Comment
Please, Sign In to add comment