Check if a string contains a substring in SQL Server 2005, using a stored procedure

CHARINDEX() searches for a substring within a larger string, and returns the position of the match, or 0 if no match is found if CHARINDEX(‘ME’,@mainString) > 0 begin –do something end Edit or from daniels answer, if you’re wanting to find a word (and not subcomponents of words), your CHARINDEX call would look like: CHARINDEX(‘ … Read more

What are the pros and cons to keeping SQL in Stored Procs versus Code [closed]

I am not a fan of stored procedures Stored Procedures are MORE maintainable because: * You don’t have to recompile your C# app whenever you want to change some SQL You’ll end up recompiling it anyway when datatypes change, or you want to return an extra column, or whatever. The number of times you can … Read more

SQL Server: Query fast, but slow from procedure

I had the same problem as the original poster but the quoted answer did not solve the problem for me. The query still ran really slow from a stored procedure. I found another answer here “Parameter Sniffing”, Thanks Omnibuzz. Boils down to using “local Variables” in your stored procedure queries, but read the original for … Read more

How to check if a stored procedure exists before creating it

I realize this has already been marked as answered, but we used to do it like this: IF NOT EXISTS (SELECT * FROM sys.objects WHERE type=”P” AND OBJECT_ID = OBJECT_ID(‘dbo.MyProc’)) exec(‘CREATE PROCEDURE [dbo].[MyProc] AS BEGIN SET NOCOUNT ON; END’) GO ALTER PROCEDURE [dbo].[MyProc] AS …. Just to avoid dropping the procedure.

How to pass an array into a SQL Server stored procedure

SQL Server 2016 (or newer) You can pass in a delimited list or JSON and use STRING_SPLIT() or OPENJSON(). STRING_SPLIT(): CREATE PROCEDURE dbo.DoSomethingWithEmployees @List varchar(max) AS BEGIN SET NOCOUNT ON; SELECT value FROM STRING_SPLIT(@List, ‘,’); END GO EXEC dbo.DoSomethingWithEmployees @List=”1,2,3″; OPENJSON(): CREATE PROCEDURE dbo.DoSomethingWithEmployees @List varchar(max) AS BEGIN SET NOCOUNT ON; SELECT value FROM OPENJSON(CONCAT(‘[“‘, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)