CREATE PROCEDURE [dbo].[ModuleReferenceCount]
-- Add the parameters for the stored procedure here
#module nvarchar(255)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COUNT([dbo].[Slots].[ObjectID])
FROM [Slots]
*WHERE [dbo].[Slots].[SlotValue] LIKE CONCAT(%%//, #module, %%)*
END
GO
This is what I have right now. The line in question is line 4 of the code portion, and is between ** (** not there in actual code). I have tried not using CONCAT and using multiple configurations with 'xyz', using = instead of like, etc. I don't think I am grasping the syntax properly, but if possible I want to check for (info)//(userdefined module name)(info) where info can be effectively anything, and (userdefined module name) is my parameter; so something like: abc//module1.xyz or lmn//module22//qrs
Thanks!
CREATE PROCEDURE [dbo].[ModuleReferenceCount]
-- Add the parameters for the stored procedure here
#module nvarchar(255)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE #searchtext AS NVARCHAR(255)
SET #searchtext='%'+#module+'%'
SELECT COUNT([dbo].[Slots].[ObjectID])
FROM [Slots]
WHERE [dbo].[Slots].[SlotValue] LIKE #searchtext
END
GO
Related
I'm running a robot test on one of our test environments, and every night, before the test runs, I would like to dump the database, and create it again from a template.
I know that PSQL has a solution for it:
CREATE DATABASE name
[ TEMPLATE [=] template ]
Is there a way I can do this in SQL Server, and write a script for it?
You can write any kind of functionality as text and then execute it as a variable. That way you can store your template as text, where you would define the database.
declare #example nvarchar(max)
set #example = 'create database examplebase'
exec sp_executesql #example
You can use this in a stored procedure where you can set database name ect. as a parameter.
create PROCEDURE sp_recreateDB
-- Add the parameters for the stored procedure here
#databasename nvarchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
declare #example nvarchar(max)
SET #example = N'CREATE DATABASE ' + QUOTENAME(#Databasename) + N';'
-- ect...
exec sp_executesql #example
END
GO
Please what is wrong with the procedure statement below
DECLARE #result int
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set #result = (select COUNT(*) from populate)
if (#result > 1)
Begin
insert into populate (brch, terminal_id) values(#branch, #atmid)
end
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE insertion #id varchar(50), #brch varchar(50)
-- Add the parameters for the stored procedure here
AS
BEGIN
DECLARE #result int
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set #result = (COUNT(*) from populate)
if (#result > 1)
Begin
insert into populate (brch, terminal_id) values(#id, #brch)
end
END
GO
It seems that you have confused things by first posting a piece of code that gives the error Msg 137, Level 15, State 2, Line 11 Must declare the scalar variable "#branch". and then later adding a complete procedure that gives the error Msg 156, Level 15, State 1, Procedure insertion, Line 13 Incorrect syntax near the keyword 'from'.
Please make sure that you post the real code you're using and the full error message too, otherwise people cannot help you.
Anyway, I ignored the code snippet and looked only at the procedure and as ABFORCE said, the problem is where you populate #result because your syntax is wrong. This procedure code parses without error in SQL Server 2008:
CREATE PROCEDURE insertion #id varchar(50), #brch varchar(50)
-- Add the parameters for the stored procedure here
AS
BEGIN
DECLARE #result int
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select #result = COUNT(*) from populate
if (#result > 1)
Begin
insert into populate (brch, terminal_id) values(#id, #brch)
end
END
GO
You might want to review the documentation for assigning values to variables and the SET keyword.
The problem, as it is said in the error message is that #branch and #atmid are not mentioned anywhere before their values are used.
You have declared #result and set it's value, so you need to do the same for #branch and #atmid as well, the system cannot divine the values for you.
Try this
select #result =COUNT(*) from populate
I have an ASP.NET website (c#) and in the code-behind I want to use the IN operator in SQL http://www.w3schools.com/sql/sql_in.asp to get data from my database.
The syntax is:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
I use a stored procedure to get data, inside this procedure I run, in the end, the select.
My question is how to build dynamically the (value1, value2, ...) part of the query? How do I send as SqlParameter(s) the values that appear in the IN operator?
The stored procedure is defined like this:
CREATE PROCEDURE [dbo].[GetAllUsers]
#ORGANIZATION_ID int = null
AS
BEGIN
SET NOCOUNT ON;
begin
SELECT *
from USERS
where ORGANIZATION_ID = #ORGANIZATION_ID
end
END
I'd like to replace the WHERE clause with something like:
WHERE ORGANIZATION in (xxxxxxxxxxxxx)
How to do this?
Thank you.
You can use dynamic sql:
CREATE PROCEDURE [dbo].[GetAllUsers]
#ORGANIZATION_ID varchar(max) = null
AS
BEGIN
declare #sql varchar(max)
#sql = 'SELECT *
from USERS
where ORGANIZATION_ID in ('+#ORGANIZATION_ID+')'
SET NOCOUNT ON;
begin
exec(#sql)
end
END
You have to change type of #ORGANIZATION_ID to varchar and run the procedure with
one id:
exec '1'
or list of ids:
exec '1,2,3,4'
For the application I work on... we're creating a custom logging system. The user can view logs and apply "Tags" to them (Just like how you can apply tags to questions here!)
In this example, I'm trying to get a list of all the Logs given a "Tag." I realize I can accomplish this by using joins... but this is also an exercise for me to learn Stored Procedures a little better :)
I have a stored procedure that looks something like this to select a log by the PK
ALTER PROCEDURE [dbo].[getLogByLogId]
-- Add the parameters for the stored procedure here
#ID int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT TOP 1
LOG_ID,
a.A,
a.B,
a.C
FROM dbo.LOG a
WHERE a.LOG_ID = #ID
Now I would like to call this Stored Procedure from another... something like this
ALTER PROCEDURE [dbo].[getLogsByTagName]
-- Add the parameters for the stored procedure here
#TAG nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT TOP 1000
LOG_ID --somehow store this and execute the dbo.getLogByLogId procedure here
FROM dbo.LOG_TAG a
WHERE a.TAG = #TAG
Thanks
If you have complex logic in your logbyid SP which you are trying to avoid reproducing in multiple places in your system (choice of columns, derived columns, etc), I would recommend turning that into an inline table-valued function instead (potentially without taking the ID parameter, in which case, you can actually use an ordinary view).
Then you can either join to that ITVF/view in your other stored proc (or also make another udf) which does the search or use the OUTER APPLY functionality (not as efficient).
Inline table-valued functions are basically parameterized views and can be optimized fairly easily by the optimizer.
If you want to call another sproc from within a sproc just use:
CREATE PROCEDURE myTestProc
AS
BEGIN
--Do some work in this procedure
SELECT blah FROM foo
--now call another sproc
EXEC nameOfSecondSproc
END
The only way you can achive what you are attempting is by using a CURSOR.
If this is for your learning only, then by all means, give this a go, but I would not recomend this for production.
It would go something like this
DECLARE #Table TABLE(
ID INT
)
INSERT INTO #Table SELECT 1
INSERT INTO #Table SELECT 2
INSERT INTO #Table SELECT 3
INSERT INTO #Table SELECT 4
INSERT INTO #Table SELECT 5
INSERT INTO #Table SELECT 6
DECLARE Cur CURSOR FOR
SELECT ID
FROM #Table
OPEN Cur
DECLARE #ID INT
FETCH NEXT FROM Cur INTO #ID
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #ID
FETCH NEXT FROM Cur INTO #ID
END
CLOSE Cur
DEALLOCATE Cur
By using the #ID retrieved in the WHILE loop, you can then execute the sp you wish and insert the values into a table variable.
INSERT INTO #Table EXEC sp_MySP #ID
You can call a stored procedure from another using the following syntax:
ALTER PROCEDURE [dbo].[getLogsByTagName]
-- Add the parameters for the stored procedure here
#TAG nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT TOP 1000
LOG_ID --somehow store this and execute the dbo.getLogByLogId procedure here
FROM dbo.LOG_TAG a
WHERE a.TAG = #TAG
-- Execute dbo.getLogByLogId stored procedure
DECLARE #logId INTEGER
SET #logId = <some value>
EXEC dbo.getLogByLogId #logId
END
However, the difficult part of your question is that your dbo.getLogByLogId procedure can only accept a single LogID parameter and therefore will only be able to return a single Log record. You need to return information for all Logs where the LogId has a corresponding record in the Tags table.
The correct way to do this would be to JOIN the Log and Tag tables together, like so:
SELECT *
FROM dbo.LOG_TAG a
INNER JOIN dbo.LOG b ON a.LOG_ID = b.LOG_ID
WHERE a.TAG = #TAG
If you are concerned about returning the same logId multiple times, you can use the DISTINCT keyword in the SELECT statement to filter out the duplicated logIds.
You may also be able to rewrite your dbo.getLogByLogId procedure as a user-defined function (UDF). UDFs can accept a table as a parameter and return a table result.
An introduction to user-defined functions can be found in this article.
I think I have the same problem as kcrumley describes in the question "Problem calling stored procedure from another stored procedure via classic ASP". However his question does not really include an solution, so I'll give it another shot, adding my own observations:
I have two stored procedures:
CREATE PROCEDURE return_1 AS BEGIN
SET NOCOUNT ON;
SELECT 1
END
CREATE PROCEDURE call_return_1_and_return_2 AS BEGIN
SET NOCOUNT ON;
EXEC return_1
SELECT 2
END
Note that both procedures contain "SET NOCOUNT ON". When I execute "call_return_1_and_return_2" I still get two record sets. First the value 1, then the value 2.
That throws ASP (classic VBScript ASP) off the tracks.
Any hints on how I can suppress the first result set? Why is it there even with NOCOUNT?
Skipping the first record set in ASP is not an option. I need a "database only" solution.
As Matt points out in his comment, neither solution really 'swallow' the first resultset.
I don't know why you'd want this but you can 'swallow' the result of the first exec by using a table variable. It must match the exact amount and type of the result set's columns. Like so:
CREATE PROCEDURE return_1 AS
SET NOCOUNT ON;
SELECT 1
GO
CREATE PROCEDURE call_return_1_and_return_2 AS
SET NOCOUNT ON;
DECLARE #Result TABLE (res int)
insert into #Result EXEC return_1
SELECT 2
GO
Its not the NOCOUNT thats causing this, your stored procedures have a select each so each one is coming in its own result set. This could be avoided by changing your first stored procedure to use output parameters to pass the number 1 back rather than doing a select. The second stored procedure could then examine the output parameter to get the data it needs to run.
Try something like this
CREATE PROCEDURE Proc1
(
#RetVal INT OUTPUT
)
AS
SET NOCOUNT ON
SET #RetVal = 1
CREATE PROCEDURE Proc2
AS
SET NOCOUNT ON
DECLARE #RetVal int
EXEC [dbo].[Proc1]
#RetVal = #RetVal OUTPUT
SELECT #RetVal as N'#RetVal'
Those are not return variables, but output record sets. I guess that as soon as SQL server flushes output to client, you're screwed and can't take it back.
I would solve this by adding a parameter to SP return_1, that would control if return_1 would select records or just do stuff and silently exit.