This is my SQL code
CREATE PROC sp_procedure
(#Name VARCHAR(50),
#Stock numeric)
AS
DECLARE #Code CHAR(4)
BEGIN
UPDATE tbProcedure
SET Name = #Name, Stock = #Stock
WHERE Code = #Code
SELECT * FROM tbProcedure
END
Then I execute the code like this
EXEC sp_procedure 'Name',15,2
Then I got error result saying too many argument specified.
I also tried this
EXEC sp_procedure 'Name',15
It doesn't return an error, but 0 rows affected.
I want to assign #Code value when I execute the stored procedure, is it even possible to do that ?
EDIT:
Sorry, it's actually a CHAR(4), not INT
Again sorry, I just copy paste all code without looking at it first, above is the actual code, I am very sorry for the confuse...
It looks like you are providing the parameters in a different order than you have declared them. Choice is declared before Name but you are providing Name first when executing the procedure. Also, don't you want your update statement to say "code = #choice"? The local variable is undefined and not needed.
For Microsoft SQL Server
For your problem:
(don't use sp prefix) and (#code is not a parameter so we cannot pass value, it is local variable)
If you are having some problems with IF then, make #code as parameter but pass null value when needed and make that null check in procedure.
See below to assign values:
USE [databasename]
GO
DECLARE #return_value int
EXEC #return_value = <procedurename>
#stringvariable = N'<stringvalue>',
#Flag = <integervalue>
SELECT 'Return Value' = #return_value (return value if any)
GO
I have come to a conclusion that its impossible to combine branching stored procedure if one of the branch do not use all the parameters.
And because of that I have write 2 stored procedures, below is my full code :
For insert procedure: [this is the procedure that using a variable]
CREATE PROC InsProcedure
(#Name VARCHAR(50),
#Stock numeric)
AS
DECLARE #CODE INT
DECLARE #CODE2 CHAR(4)
BEGIN
// Creating custom auto number
SET #CODE = 0
SELECT #CODE = ISNULL(MAX(CAST(KodeBarang AS INT)), 0)
FROM tbProcedure
SET #CODE2 = #CODE + 1
SET #CODE2 = REPLICATE('0', 4 - LEN(#CODE2)) + #CODE2
// End Custom auto number
INSERT INTO tbProcedure VALUES(#CODE2, #Name, #Stock)
SELECT * FROM tbProcedure
END
For update, delete, and show procedure:
CREATE PROC OtherProcedure
(#choice int, // this is for branching option later
#Code CHAR(4),
#Name VARCHAR(50),
#Stock numeric)
AS
IF #choice = 1
BEGIN
UPDATE tbProcedure
SET Name = #Name, Stock = #Stock
WHERE Code = #Code
SELECT * FROM tbProcedure
END
ELSE IF #choice = 2
DELETE FROM tbProcedure
WHERE KodeBarang = #Code
ELSE
SELECT * FROM tbProcedure
and use the procedures like this :
InsProcedure 'Laptop', 5
Result:
00001, Laptop, 5
Branch 1 [Update]
OtherProcedure 1, 'Laptops', 10, 00001
Result:
Before : 00001, Laptop, 5
After : 00001, Laptops, 10
Branch 2 [Delete]
OtherProcedure 2, '', 0, 00001 // Delete record where Code is '00001'
Branch 3 [Show]
OtherProcedure 3, '', 0, 0 // You can also use any number besides 3
First of all, your variable #Stock doesn't return decimal because it's INT variable, you might try float or something else. It's the reason why it shows an error if you give it "15,2"
WHERE Code = #Code
And 0 rows affected was caused because you didn't set that variable properly.
You have to assign a valid value to it.
Related
I'm new to stored procedures world and in one of my projects I'm trying to implement stored procedure for the search process. I'm not sure if everything I created is correct. Also I'm wondering if this code has any leaks or vulnerability for SQL injection. Here is example of my stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Search_Dictionary]
#FilterBy INT = NULL,
#Name VARCHAR(50) = NULL,
#Code CHAR(2) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT RecID, Status, Code, Name
FROM Dictionary
WHERE
(#FilterBy = 1 AND Name LIKE '%'+#Name+'%')
OR (#FilterBy = 2 AND Code = #Code)
OR (#FilterBy = 3 AND #Name IS NULL AND #Code IS NULL);
END
Here is example on how I call this procedure:
EXEC Search_Dictionary #FilterBy = 1, #Name = "Grant", #Code = NULL;
I just want to prevent if for example filter by is 2 that should search query by Code column returns any result if user pass word Grant. In that case should return 0 records. Also if anyone have any suggestions on how to improve the code please let me know. Thank you.
I have an SQL Procedure like below:
CREATE PROCEDURE [dbo].[ExampleSP]
#ID int,
#name varchar(30),
#Counter int output
AS
BEGIN
SET NOCOUNT ON;
---- ********************************************************************************************************
if (#name is not null)
BEGIN
update People
set name = #name
where Id = #ID
END
set #Counter = ##ROWCOUNT
END
What I want is here to count how many rows entered the if condition!
In SSIS -> I have a common variable, called RecordCounter = 0 by default.
I have a Data Flow Task in my Control Flow.
In the Data Flow: -> I have a Flat File Source, reading data from a .dat file.
Example data in the file:
ID|NAME
1 Jack
2
3 Mike
Then, I have Derived Column, adding RecordCounter as a column to the data.
RecordCounter <add as new column> #[User::RecordCounter] four-byte signed integer [DT_I4]
Then, I have OLE DB Command, which enables the data goes row by row to my SP. SQL Command for that is:
EXEC [dbo].[ProcessClientRegistryRecord] ?,?,? OUTPUT
And finally, I have a Row Count item, mapping the variable User::RecordCounter
Here is the figure:
When I run this, the RecordCounter is showing the number of total data in my file, instead how many of them went into the IF block in the StoredProcedure. It should return 2 in this situation for example, not 3. Where is my mistake, how should I fix it? Any help would be appreciated!
After some explanation from you I think I know where you are going wrong.
I think you have done everything right except the logic in the stored procedure.
Your Stored procedure should look like....
CREATE PROCEDURE [dbo].[ExampleSP]
#ID int,
#name varchar(30),
#Counter int output
AS
BEGIN
SET NOCOUNT ON;
IF (#name is not null)
BEGIN
update People
set name = #name
where Id = #ID
SET #Counter = ##ROWCOUNT;
END
END
Finally in your SSIS package you can add an aggregate task to sum all the rows update from the derived column. You package should look something like...
In the OLE DB Transformation on the Component Properties tab should have the call to stored procedure as
Exec [dbo].[ExampleSP] ? , ? , ? OUTPUT
And finally on the Column Mapping tab, the OUTPUT parameter should be mapped to your derived column:
Assigning value out of the context seems not to work at least you do a final assignment.
**Declare #Id int =?, #rowCount int =?;
EXEC Exec [dbo].[ExampleSP] #Id , #rowCount OUTPUT;
SELECT ? = #rowCount;**
Also, be aware of mapping starts at 0.
The following is my stored procedure to update a column in SQL SERVER
ALTER PROCEDURE [dbo].[AspPageUpdate]
(#type varchar(50),#comp varchar(50),
#place varchar(50))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #tid varchar;
DECLARE #ph int;
SET #ph = CAST(#place AS int);
select #tid = Type_Id
from TypeTable
where Type_Name = #type
UPDATE TypeSetupTable
SET PLACE_HOLDERS = #ph
WHERE complexity = #comp
AND Type_Id = #tid
END
But the table is not getting updated. I think the problem is with Quotes(Strings need to be in quotes, right?).
If i'm giving static values, it is execting, like:
UPDATE TypeSetupTable SET PLACE_HOLDERS = #ph WHERE complexity = 'Simple' AND Type_Id = 'SSRS'
Please tell me a solution.
Thanks in Advance.
you didn't set the size of the variable #tid.
Are you sure of the content of that variable while executing the stored procedure?
Try to put a raiserror(#tid,15,1) and check the content of that variable.
There are blogs about the habit not to size varchar variables.
It is also officially documented that the size of unsized varchars is 1.
I have a stored procedure where I will have an output parameter. The query works and when I run the query inside SQL Server Management Studio, I get the correct answer. My problem is assigning the answer to my output parameter. Here is the stored procedure:
ALTER PROCEDURE [dbo].[RDusp_Report_Impact]
-- Add the parameters for the stored procedure here
#SiteID int,
#RiskCount int output
AS
BEGIN
SET NOCOUNT ON;
select sum(cnt) as mytotal from
(
select count(Impact.Rating) as cnt from Impact, Likelihood, Exposure where
Impact.SiteID=2
and Exposure.SiteID = 2 and Impact.Rating > 3 and Likelihood.Rating > 3
and Exposure.ImpactID = Impact.ImpactID and exposure.LikelihoodID = Likelihood.LikelihoodID
) as c
END
I try to assign #RiskCount to be the value in mytotal, but it says that the column doesn't exist. I just want to get that one result back. Shouldn't be too difficult, just a syntax thing that I can't get. Thanks.
Modify your query like this (the crucial part is the 1st line of the SELECT statement - select #RiskCount = sum(cnt):
ALTER PROCEDURE [dbo].[RDusp_Report_Impact]
-- Add the parameters for the stored procedure here
#SiteID int,
#RiskCount int output
AS
BEGIN
SET NOCOUNT ON;
select #RiskCount = sum(cnt)
from
( select count(Impact.Rating) as cnt
from Impact, Likelihood, Exposure
where Impact.SiteID=2
and Exposure.SiteID = 2
and Impact.Rating > 3
and Likelihood.Rating > 3
and Exposure.ImpactID = Impact.ImpactID
and exposure.LikelihoodID = Likelihood.LikelihoodID ) as c
END
Execute it like this:
DECLARE #rc int
EXEC [dbo].[RDusp_Report_Impact] 123, #rc output -- 123 is an example #SiteID value
SELECT #rc
Helo,
My question is I have one Stored Procedure in SQL Server that returns counts of a field. I want to store the results of this Stored Procedure in a variable (scalar?) of a different stored procedure.
sp_My_Other_SP:
CREATE PROCEDURE [dbo].sp_My_Other_SP
#variable int OUTPUT -- The returned count
AS
BEGIN -- SP
SET NOCOUNT ON;
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
END -- SP
I currently do it like:
DECLARE #count int
EXEC sp_My_Other_SP #count OUTPUT
Then I use it like
IF (#count > 0)
BEGIN
...
END
However its returning the other Stored Procedure results as well as the main Stored Procedure results which is a problem in my .NET application.
-----------
NoColName
-----------
14
-----------
MyCol
-----------
abc
cde
efg
(Above is an attempted representation of the results sets returned)
I would like to know if there is a way to store the results of a Stored Procedure into a variable that doesn't also output it.
Thanks for any help.
You can capture the results of the stored procedure into a temp table so it is not returned by the calling stored procedure.
create table #temp (id int, val varchar(100))
insert into #temp
exec sp_My_Other_SP #value, #value, #value, #count OUTPUT
Well, the easiest way to fix this is to recode the stored proc so that the select statement that returns the 'other' result set you don't want in this case is conditionally extecuted, only when you are NOT asking for the count
Add another parameter called #GetCount
#GetCount TinyInt Defualt = 0 // or
#GetCount Bit Default = 0
Then
instead of just
Select ...
write
If #GetCount = 1
Select ...
Have you tried changing
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
to
SELECT #variable = COUNT(*) FROM blah"
-- don't do EXEC(#SQL)
?
THE FIRST PROCEDURE:
CREATE PROC DD43
#ID INT OUTPUT AS
(SELECT #ID=COUNT(*) FROM CS2)
SECOND PROCEDURE:
CREATE PROC DD45 AS
DECLARE #COUNT INT
DECLARE #COUN INT
EXEC DD43 #COUN OUT --CALLING THE FIRST PROCEDURE
SET #COUNT= (SELECT #COUN)
SELECT #COUNT
EXEC DD45