Showing posts with label int. Show all posts
Showing posts with label int. Show all posts

Sunday, March 11, 2012

calling a stored procedure

Hi, i've had this query method:

34 public void AddDagVerslagCategorie(int logID, HistoriekDetail historiekDetail)
35 {
36 SqlConnection oConn =new SqlConnection(_connectionString);
37 string strSql ="Insert into LogDetail (LogID, CategorieID, Inhoud)";
38 strSql +="values(@.logID, @.categorieID, @.inhoud)";
39 SqlCommand oCmd =new SqlCommand(strSql, oConn);
40 oCmd.Parameters.Add(new SqlParameter("@.logID", SqlDbType.Int)).Value = logID;
41 oCmd.Parameters.Add(new SqlParameter("@.categorieID", SqlDbType.Int)).Value = historiekDetail.CategorieID;
42 oCmd.Parameters.Add(new SqlParameter("@.inhoud", SqlDbType.VarChar, 100)).Value = historiekDetail.Inhoud;
43
44 try
45 {
46 oConn.Open();
47 int rowsAffected = oCmd.ExecuteNonQuery();
48 if (rowsAffected == 0)throw new ApplicationException("Fout toevoegen historiek detail");
49 oCmd.CommandText ="select @.@.IDENTITY";
50 oCmd.Parameters.Clear();
51 historiekDetail.HistoriekDetailID = (int)(decimal)oCmd.ExecuteScalar();
52 }
53 catch (Exception ex)
54 {
55 throw new ApplicationException("Fout toevoegen historiek detail: " + ex.Message);
56 }
57 finally
58 {
59 if (oConn.State == ConnectionState.Open) oConn.Close();
60 }
61 }

which i've converted to a stored procedure:

 
1ALTER PROCEDURE [dbo].[insert_DagVerslagDetail]2-- Add the parametersfor the stored procedure here3@.dagverslagdetailIDint,4 @.logIDint,5@.categorieIDint,6 @.inhoud varchar(100)7AS8BEGIN9-- SET NOCOUNT ON added to prevent extra result sets from10-- interfering with SELECT statements.11SET NOCOUNT ON;12 SET @.dagverslagdetailID = SCOPE_IDENTITY()1314 -- Insert statementsfor procedure here15BEGIN TRANSACTION16 INSERT LogDetail (LogID, CategorieID, Inhoud)17 VALUES(@.logID, @.categorieID, @.inhoud)18 COMMIT TRANSACTION19END

Now i would like to call that stored procedure in my previous method, so i've changed it to this:

1public void AddDagVerslagCategorie(int logID, HistoriekDetail historiekDetail)2 {3 SqlConnection oConn =new SqlConnection(_connectionString);4string strSql ="insert_DagVerslagDetail";5 strSql +="values(@.logID, @.categorieID, @.inhoud)";6 SqlCommand oCmd =new SqlCommand(strSql, oConn);7 oCmd.CommandType = CommandType.StoredProcedure;8 oCmd.Parameters.Add(new SqlParameter("@.logID", SqlDbType.Int)).Value = logID;9 oCmd.Parameters.Add(new SqlParameter("@.categorieID", SqlDbType.Int)).Value = historiekDetail.CategorieID;10 oCmd.Parameters.Add(new SqlParameter("@.inhoud", SqlDbType.VarChar, 100)).Value = historiekDetail.Inhoud;1112try13 {14 oConn.Open();15int rowsAffected = oCmd.ExecuteNonQuery();16if (rowsAffected == 0)throw new ApplicationException("Fout toevoegen historiek detail");17 oCmd.CommandText ="select @.@.IDENTITY";18 oCmd.Parameters.Clear();19 historiekDetail.HistoriekDetailID = (int)(decimal)oCmd.ExecuteScalar();20 }21catch (Exception ex)22 {23throw new ApplicationException("Fout toevoegen historiek detail: " + ex.Message);24 }25finally26 {27if (oConn.State == ConnectionState.Open) oConn.Close();28 }29 }

Do i still need the lines17 oCmd.CommandText ="select @.@.IDENTITY";

19 historiekDetail.HistoriekDetailID = (int)(decimal)oCmd.ExecuteScalar();

Because i've declared the identity in my stored procedure

Modify your procedure to return you identity column as output parameter, and by the way you do not have to use transaction around single insert statement

or do:

ALTER PROCEDURE [dbo].[insert_DagVerslagDetail]
2 -- Add the parametersfor the stored procedure here
3 @.dagverslagdetailIDint,
4 @.logIDint,
5 @.categorieIDint,
6 @.inhoud varchar(100)
7 AS
8 BEGIN
9 -- SET NOCOUNT ON added to prevent extra result sets from
10 -- interfering with SELECT statements.
11 SET NOCOUNT ON;
12 SET @.dagverslagdetailID = SCOPE_IDENTITY()
13
14 -- Insert statementsfor procedure here

16 INSERT LogDetail (LogID, CategorieID, Inhoud)
17 VALUES(@.logID, @.categorieID, @.inhoud)
select SCOPE_IDENTITY()
19 END

and get your identity at one call to stored procedure

Thursday, March 8, 2012

callin functions inside queries

im trying to make this function in tsql

create function isolatedLeptons
(@.idevent INT)
Returns TABLE
AS
Return select l.*
from lepton as l, event as e
where e.id = l.eventid and pt(l.id) > 7.0 and abs(eta(l.id))<2.4
END

GO

select * from lepton;

but it trow me this error

Msg 195, Level 15, State 10, Procedure isolatedLeptons, Line 9
'pt' is not a recognized built-in function name.

pt is defined as

create function pt
(@.idpt INT)
Returns Real
AS
BEGIN
Return ( select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id)
END

GO

Hi,

you need to use dbo.pt(l.id).

--
Regards,
Daniel Kuppitz

|||

Please do not use scalar UDFs for simple tasks like this. It is overkill in terms of performance and probably manageability too. Use the query expression inline like:

create function isolatedLeptons
(@.idevent INT)
Returns TABLE
AS
Return select l.*
from lepton as l, event as e
where e.id = l.eventid

and (select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where a.id = l.id) > 7.0

and abs(eta(l.id))<2.4

Or if you want to go the UDF route use an inline TVF instead like:

create function pt
(@.idpt INT)
returns table
AS
return ( select sqrt(a.px*a.px + a.py*a.py) as val
from abstractparticle as a
where @.idpt = a.id)
GO

create function isolatedLeptons
(@.idevent INT)
Returns TABLE
AS
Return select l.*
from lepton as l, event as e
where e.id = l.eventid

and (select a.val

from pt(l.id)) > 7.0

and abs(eta(l.id))<2.4

Wednesday, March 7, 2012

Call stored procedure from another stored procedure?

How do I call this stored procedure:

CREATE Procedure hentSpecialPris
(
@.varenr int,
@.cvr int,
@.pris decimal(7,2) OUTPUT
)

AS

DECLARE @.antal int

SET @.antal = 0

SET @.antal = (SELECT COUNT(*) FROM rabat WHERE varenr=@.varenr and cvr = @.cvr)

IF @.antal < 1
BEGIN
SET @.pris = (SELECT pris FROM vare WHERE varenr=@.varenr)
END

ELSE
BEGIN
SET @.pris = (SELECT pris FROM rabat WHERE varenr=@.varenr and cvr = @.cvr)
END

GO

from another stored procedure?? I want to get the output value of the stored procedure?


CREATE PROCEDURE OtherProc @.varent int,@.cvr int
AS

DECLARE @.pris decimal(7,2)

EXEC hentSpecialPris @.varent,@.cvt,@.pris OUTPUT

GO

You could also make this SP a user defined funciton, if it will always be called from another SP.