Good day
I have problem with my store procedure as follow:
I need to know wchich manager have which employee
My statement with function WITH as SELECT working fine ,but
my problem is with procedure when iam caling back Iam geting 0 rows back.
I think should by problem with Function LIKE when Iam creating procerure.
Thank you for opinions
create procedure test #Name varchar(200) as
WITH n(Surname,Name) AS
(SELECT Surname,Name
FROM [dbo].[phonebook]
WHERE Name like '%#Name' --I need to be like here beause before name is XT245*/ SUBCODE
UNION ALL
SELECT subs.Surname, subs.Name
FROM [dbo].[phonebook] as subs, n
WHERE n.Surname = subs.Boss)
SELECT Name FROM n
exec test 'XX37485*/John'
This expression:
WHERE Name like '%#Name' --I need to be like here because before name is XT245*/ SUBCODE
is looking for names that have exactly that pattern of characters . . . # N a m e. There is no variable substitution.
If you want to use like, then:
WHERE Name like '%' + #Name
Note that this requires that the pattern end with #Name, so it will not match #Name anywhere in the string.
Related
I want to execute a dynamic SQL statement, which searches for names whose last name is always a constant and first name is a variable.
Here is a query I have written for selecting a row with name='Test lastname'.
EXECUTE 'SELECT name FROM users
WHERE name=$1 lastname'
USING ('Test');
This generates a syntax error. Is it possible to do this?
I think you need something like this:
EXECUTE 'SELECT user_id FROM users
WHERE name=$1'
USING Test||' lastname' ;
Here Test is variable and 'lastname' is hard coded value
Also another way is as #JorgeCampos mentioned:
...WHERE name=$1 || '' lastname''' USING 'Test';
I am testing contains method in store procedure. I would like to see data result like:
Example Result for John:
John Nick
Papa John
Harly John Fredy
I don't want to use LIKE method. Can we use starts with or ends with (*) operator with parameters in SP?
CONTAINS(name,#name) // query is working
but if i can try like this:
CONTAINS(name, '"john" OR "john*"') // query is working
CONTAINS(name,'"#name" OR "#name*"') // query is not working
With parameters same query is not working in SP. Is it posibble to do this in SP?
Thanks,
With CONTAINS, in order to pass a wildcard through with a variable, you need to make the wildcard a part of the variable.
The easiest way to do it within a stored procedure is adding a variable (or modifying your current variable).
For example,
DECLARE #nameX NVARCHAR(4000) = '"' + #name + '" OR "' + #name + '*"'
...
CONTAINS(name, #nameX)
I created a stored procedure (spBalanceRange) with 2 optional parameters. They've been set to a default value and the sp works fine when I pass only 1 value per parameter by position. However, I have a situation where I'm trying to pass, by position, two strings immediately followed by a wildcard. I want the user to be able to search for Vendor names that start with either 'C%' or 'F%'. Here's the gist of the CREATE PROC statement:
CREATE PROC spBalanceRange
#VendorVar varchar(40) = '%',
#BalanceMin money = 1.0
...
Here's what I've tried so far, but doesn't work:
EXEC spBalanceRange '(C%|F%)', 200.00;
EXEC spBalanceRange 'C%|F%', 200.00;
Is there a way to check for 2 or more string values with a wildcard when passed by position? Thanks.
EDIT: According to your comments you are looking for the first letter of a vendor's name only.
In this special case I could suggest an easy, not well performing but really simple approach. CHARINDEX returns a number greater than zero, if a character appears within a string. So you just have to pass in all your lookup-first-characters as a simple "chain":
DECLARE #DummyVendors TABLE(VendorName VARCHAR(100));
INSERT INTO #DummyVendors VALUES
('Camel Industries')
,('Fritz and Fox')
,('some other');
DECLARE #ListOfFirstLetters VARCHAR(100)='CF';
SELECT VendorName
FROM #DummyVendors AS dv
WHERE CHARINDEX(LEFT(dv.VendorName,1),#ListOfFirstLetters)>0
This was the former answer
Checking against more than one value needs either a dedicated list of compares
WHERE val=#prm1 OR val=#prm2 OR ... (you know the count before)
...or you use the IN-clause
WHERE LEFT(VenoderName,1) IN ('C','F', ...)
...but you cannot pass the IN-list with a parameter like ... IN(#allValues)
You might think about a created TYPE to pass in all your values like a table and use an INNER JOIN as filter: https://stackoverflow.com/a/337864/5089204 (and a lot of other examples there...)
Or you might think of dynamic SQL: https://stackoverflow.com/a/5192765/5089204
And last but not least you might think of one of the many split string approaches. This is one of my own answers, section "dynamic IN-statement": https://stackoverflow.com/a/33658220/5089204
I'm answering my own question, and maybe other solutions exist but here is what had to happen with my stored procedure in order to pass variables by position:
CREATE PROC spBalanceRange
#VendorVar varchar(40) = '%',
#BalanceMin money = 1.0
AS
IF (#VendorVar = '%' AND #BalanceMin IS NULL OR #BalanceMin = '')
BEGIN
PRINT 'BalanceMin cannot be null.';
END
IF (#VendorVar = % AND #BalanceMin IS NOT NULL)
BEGIN
(sql statement using parameters)
END
EXEC spBalanceRange '[C,F]%', 200.00;
That's what I know.
I have procedure it is working but i want to improve it, by giving ability to search by couple letters in word. Procedure:
alter PROCEDURE SearchByIncIDroNameOfClientoStreetorEmpID
#IncID int,
#NameOfClient nvarchar (60),
#StrID int,
#EmpID int,
#date date
as
select IncID,NameOfClient,EnterDate,StrName ,AppartmentNumber as 'appartment',Summary,IncStatus,e.LastName,EndDate
from Incident i
JOIN Streets s on i.StrID=s.StrID
join Employee e on i.EmpID=e.EmpID
where IncID =#IncID or NameOfClient = #NameOfClient or s.StrID = #StrID or i.EmpID=#EmpID or EnterDate =#date
go
I need modify this part NameOfClient = #NameOfClient so it will show records which begins with provided letter. so right now if i want to searh for John i have to wright John but I want for example if i will give just Jo it should return me all of the records which starts from Jo
I was trying to add % sign before #NameOfClient but it doesn't work. Any Ideas how I can do it?
You need to use like operator:
NameOfClient like #NameOfClient+'%'
NameOfClient like #NameOfClient + '%'
Doesn't this works?
You must use the LIKE operator:
WHERE NameOfClient LIKE #NameOfClient
I have two records in my person table in my database, their last names are "تحصیلداری" and "موقر".
when I use
select * from Person where Lastname like N'%ی%'
the record containing "ی" character in last name returns, which is exactly what I need, but I need a little change. I need to use the word between % and % as a parameter.
Something like :
create procedure usp_GetPersonsWhoseNameContains(#LN nvarchar(50))
as
begin
select * from Person where Lastname like N'%'+#LN+'%'
end
declare #LastName nvarchar(50) = 'ی'
exec usp_GetPersonsWhoseNameContains(#LastName)
but it does not work this way, and in my searches I couldn't find the appropriate solution. Does anyone know how to
Try
create procedure usp_GetPersonsWhoseNameContains(#LN nvarchar(50))
as
begin
select * from Person where Lastname like N'%'+#LN+'%'
end
declare #LastName nvarchar(50) = N'ی'
exec usp_GetPersonsWhoseNameContains(#LastName)
Maybe you noticed that SQL Server uses N to any quoted string input when generating DDL scripts for you.