Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
my sql function is not working parameter value. If provide hard code value it will give me results.
ALTER function [dbo].[team_concat] (#input varchar)
returns varchar(8000)
as
BEGIN
declare #putout varchar(8000)
set #putout = null
-- select #putout = COALESCE(IsNull(#putout+ ', ', ''), '') + team_residence_location from programs where team_combo = 'Hartford CIS / Coaching'
select #putout = COALESCE(IsNull(#putout+ ', ', ''), '') + team_residence_location from programs where team_combo = #input
return #putout
Try this:
ALTER function [dbo].[team_concat] (#input varchar(100)) --- specify length
returns varchar(8000)
as
BEGIN
declare #putout varchar(8000)
set #putout = null
-- select #putout = COALESCE(IsNull(#putout+ ', ', ''), '') + team_residence_location from programs where team_combo = 'Hartford CIS / Coaching'
select #putout = COALESCE(IsNull(#putout+ ', ', ''), '') + team_residence_location from programs where team_combo = #input
return #putout
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to create a stored procedure for creating login into SQL Server. The stored procedure is complied successfully but I am getting error when I am executing the stored procedure.
CREATE OR ALTER PROCEDURE dbo.sp_login_1
(#userId varchar(15),
#passwd varchar(15),
#db varchar(15))
AS
BEGIN
DECLARE #stmt NVARCHAR(1000)
SET #stmt = 'CREATE LOGIN ' + #userId +
' WITH PASSWORD = ' + #passwd + ', ' +
' DEFAULT_DATABASE = ' + #db + '; ' +
'CREATE USER' + #userId +
' FROM LOGIN ' + #userId + ';' +
'ALTER ROLE groupAdmin ADD MEMBER' + #userId + ';'
EXEC sp_executesql #stmt;
END;
exec dbo.sp_login_1 #userId='user1', #passwd ='pwd', #db ='dbo.myDB';
The following error is displayed.
Msg 102, Level 15, State 1, Line 159
Incorrect syntax near 'pwd'.
Msg 102, Level 15, State 1, Line 159
Incorrect syntax near ';'.
You must add single quotes for the password field. Best to do this with QUOTENAME
SET #stmt = 'CREATE LOGIN ' + QUOTENAME(#userId) +
' WITH PASSWORD = ' + QUOTENAME(#passwd, '''') + ', ' +
' DEFAULT_DATABASE = ' + QUOTENAME(#db) + '; ' +
'CREATE USER' + QUOTENAME(#userId) +
' FROM LOGIN ' + QUOTENAME(#userId) + ';' +
'ALTER ROLE groupAdmin ADD MEMBER ' + QUOTENAME(#userId) + ';'
The reason the error message refers to pwd (which is not in the script) is because it clearly passed the pwd as a #passwd parameter.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Get pattern matched sub string from string in SQL server
for example my string is 'Test{Ab}{CV}Ad testing'
I need out put of above string 'Ab' and 'CV' which is available in '{}'
I need out put without brackets and selected sub string in rows
DECLARE #Template NVARCHAR(MAX) = 'Test{Ab}{CV}Ad testing'
The following will work even when you have more than two {} placeholders:
DECLARE #Template NVARCHAR(MAX) = 'Test{Ab}{CV}Ad testing'
DECLARE #counter INT = 1
DECLARE #inside INT = 0
DECLARE #curr VARCHAR(1) = ''
DECLARE #output VARCHAR(MAX) = ''
WHILE(#counter < LEN(#Template))
BEGIN
SET #curr = SUBSTRING(#Template, #counter, #counter)
SET #inside = CASE
WHEN #curr = '{' THEN 1
WHEN #curr = '}' THEN 0
ELSE #inside
END
SET #output = CASE
WHEN #inside = 1 THEN #output + #curr
WHEN #curr = '}' THEN #output + #curr + '_'
ELSE #output
END
SET #counter = #counter + 1
--SELECT #curr, #inside, #output
END
SELECT * FROM STRING_SPLIT( #output ,'_')
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
If it was a big query would it be faster on the second stored procedure ?
CREATE PROCEDURE Customers_GetCustomer
#CustId CHAR(5),
#type int,
#search nvarchar
AS
BEGIN
DECLARE #SQL NVARCHAR(2000)
SET #SQL = 'SELECT ContactName FROM Customers WHERE CustomerId = '+CONVERT(nvarchar(20), #CustId)
if(#type = 1)
BEGIN
#SQL += 'AND NAME='+#search
END
if(#type = 2)
BEGIN
#SQL += 'AND ADDRESS='+#search
END
EXEC sp_executesql #SQL
END
Versus:
CREATE PROCEDURE Customers_GetCustomer
#CustId CHAR(5),
#type int,
#search nvarchar
AS
BEGIN
SELECT ContactName FROM Customers
WHERE CustomerId = #CustId
AND (NAME = #search OR #type <> 1)
AND (ADDRESS = #search OR #type <> 2)
END
What is the better choice between first and second?
With a simple query like this, there will be virtually no difference even at 1000s of executions/sec. (Based on my own testing when I had the same question.)
Assuming it's properly parameterised, a complex query will only have the additional overhead of hashing the longer string to match the execution cache.
But I'd suggest testing it yourself, with https://www.brentozar.com/archive/2015/05/how-to-fake-load-tests-with-sqlquerystress/ for example.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I need to execute a SQL like bellow inside a SP.
DECLARE
#varSearchQ VARCHAR(100),
#varFieldName VARCHAR(100),
#varTableName VARCHAR(100),
#Sql VARCHAR(Max)
SET #varSearchQ='000'
SET #varFieldName='varConsoleID'
SET #varTableName='FF.ConsoleDT'
SET #Sql = N'SELECT ' + #varFieldName + '
FROM ' + #varTableName + '
WHERE ' + #varFieldName + ' LIKE %' + #varSearchQ + '%'
I found a way like EXEC sp_executesql #Sql. But I'm getting errors.
Try this
You Should Use Print for Debugging Statments
Create proc MYPROC
as
BEGIN
DECLARE
#varSearchQ VARCHAR(100),
#varFieldName VARCHAR(100),
#varTableName VARCHAR(100),
#Sql VARCHAR(Max)
SET #varSearchQ='000'
SET #varFieldName='varConsoleID'
SET #varTableName='FF.ConsoleDT'
SET #Sql = N'SELECT ' + #varFieldName + '
FROM ' + #varTableName + '
WHERE ' + #varFieldName + ' LIKE ''%' + #varSearchQ + '%'''
print (#Sql)
EXEC (#Sql)
END
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've table named EMPLOYEE which contains First Name, Last Name and Employee ID. Now I want to get rows when I search for First Name alone or Last name alone or may be both. How to do that?
You can use the following query. I assume you have 2 paramers #FirstName and #LastName which can be NULL if you don't want to search by them.
SELECT * FROM EMPLOYEE
WHERE ([First Name] = #FirstName OR #FirstName IS NULL)
AND ([Last Name] = #LastName OR #LastName IS NULL)
I would use dynamic queries:
DECLARE #FirstName NVARCHAR(50), #LastName NVARCHAR(50);
SET #FirstName = 'John';
SET #LastName = 'Johnson';
DECLARE #SqlStatement NVARCHAR(MAX), #SqlParamters NVARCHAR(MAX);
SET #SqlStatement = N'SELECT e.EmployeeID, e.FirstName, e.LastName FROM dbo.Employee e
'
+ CASE WHEN #FirstName IS NOT NULL OR #LastName IS NOT NULL THEN 'WHERE '
+ CASE WHEN #FirstName IS NOT NULL THEN 'e.FirstName = #pFirstName'
+ CASE WHEN #LastName IS NOT NULL THEN 'e.LastName = #pLastName';
-- PRINT #SqlStatement;
EXEC sp_executesql
#SqlStatement,
N'#pFirstName NVARCHAR(50), #pLastName NVARCHAR(50)',
#pFirstName = #FirstName,
#pLastName = #LastName;
Indexing: if you have the following index
CREATE INDEX IX_Employee_LastName_FirstName
On dbo.Employee (LastName, FirstName);
then only the following predicates are SARGable (Index Seek):
WHERE e.LastName = #pLastName
and
WHERE e.LastName = #pLastName AND e.FirstName = #pFirstName
but the following predicate isn't SARGable (Index Scan):
WHERE e.FirstName = #pFirstName