Showing posts with label identity. Show all posts
Showing posts with label identity. Show all posts

Wednesday, March 21, 2012

Grab IDENTITY from called stored procedure for use in second stored procedure in ASP.NET p

I have a sub that passes values from my form to my stored procedure. The stored procedure passes back an @.@.IDENTITY but I'm not sure how to grab that in my asp page and then pass that to my next called procedure from my aspx page. Here's where I'm stuck:

Public Sub InsertOrder()

Conn.Open()

cmd = New SqlCommand("Add_NewOrder", Conn)
cmd.CommandType = CommandType.StoredProcedure

' pass customer info to stored proc
cmd.Parameters.Add("@.FirstName", txtFName.Text)
cmd.Parameters.Add("@.LastName", txtLName.Text)
cmd.Parameters.Add("@.AddressLine1", txtStreet.Text)
cmd.Parameters.Add("@.CityID", dropdown_city.SelectedValue)
cmd.Parameters.Add("@.Zip", intZip.Text)
cmd.Parameters.Add("@.EmailPrefix", txtEmailPre.Text)
cmd.Parameters.Add("@.EmailSuffix", txtEmailSuf.Text)
cmd.Parameters.Add("@.PhoneAreaCode", txtPhoneArea.Text)
cmd.Parameters.Add("@.PhonePrefix", txtPhonePre.Text)
cmd.Parameters.Add("@.PhoneSuffix", txtPhoneSuf.Text)

' pass order info to stored proc
cmd.Parameters.Add("@.NumberOfPeopleID", dropdown_people.SelectedValue)

cmd.Parameters.Add("@.BeanOptionID", dropdown_beans.SelectedValue)
cmd.Parameters.Add("@.TortillaOptionID", dropdown_tortilla.SelectedValue)

'Session.Add("FirstName", txtFName.Text)

cmd.ExecuteNonQuery()

cmd = New SqlCommand("Add_EntreeItems", Conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@.CateringOrderID", get identity from previous stored proc) <--------

Dim li As ListItem
Dim p As SqlParameter = cmd.Parameters.Add("@.EntreeID", Data.SqlDbType.VarChar)
For Each li In chbxl_entrees.Items
If li.Selected Then
p.Value = li.Value
cmd.ExecuteNonQuery()
End If
Next

Conn.Close()

I want to somehow grab the @.CateringOrderID that was created as an end product of my first called stored procedure (Add_NewOrder) and pass that to my second stored procedure (Add_EntreeItems)

Sorry that I don't know the exact syntax off hand, but if your stored procedure is returning the @.@.Identity as it's return, then you need to add the return parameter to the first command.

It'd be something like (sorry again, this is from memory):

cmd.Parameters.Add(New SqlParameter("@.CateringOrderID",RETURN_VALUE)) or something similiar, then at later add

dim ret as long = cmd.Parameters("@.CateringOrderID").value

Or something very similiar.

If your stored procedure is using an output parameter, you need to do the same thing, but instead of calling it RETURN_VALUE, it's like OUTPUT_VALUE or something. There is a more descriptive way of adding parameters that you can tell it if it's return value type parameter, an input type, or an output type. Once you find that, that's 90% of your solution.

|||thanks for your input, still working on it.|||

right now I'm trying to use this to obtain the returned @.@.IDENTITY from my first stored proc:

Dim NewCateringOrderIDAsInteger =CType(cmd.ExecuteScalar(),Integer)

|||Yes, that will work if the identity is the first and only result the stored procedure generates. To turn off empty resultsets (From inserts, updates, etc that go on in the stored procedure), place a SET NOCOUNT ON at the beginning of the stored procedure, and place a SET NOCOUNT OFF right before your SELECT SCOPE_IDENTITY(). Then optionally add SET NOCOUNT ON again if there are any other database modification statements after that, and finally SET NOCOUNT OFF at the very end.|||

Here we go, thanks for the start

http://aspnet.4guysfromrolla.com/articles/062905-1.aspx

|||You can do something like this:

SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand("sproc_name",myConnection);

// Attach the parameters to the myCommand object

Set the parameter direction of the commad object to returnValue

myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();

int identity = (int) myCommand.Parameters["MyReturnValue"].value;

// now that you have identity you can send into next stored procedure

Also remember that you can also send the values from one stored procedure to another stored procedure using T-SQL queries this way you dont need to return the identity to the presentation layer.

CREATE PROCEDURE #test_proc
AS
INSERT INTO #test VALUES(123)

RETURN SCOPE_IDENTITY()|||Here is another way which simply calls one stored procedure from another stored procedure:

CREATE TABLE #Names (k1 int identity(1,1) , name varchar(20) )

CREATE TABLE #Phone (k2 int identity(1,1), phone varchar(20), k1 int )

CREATE PROCEDURE #insert_name

@.Name varchar(20)

AS

DECLARE @.returnValue int

INSERT INTO #Names VALUES(@.Name)

SET @.returnValue = SCOPE_IDENTITY()

EXEC #insert_phone @.nameID = @.returnValue

EXEC #insert_name @.Name = 'AzamSharp'

SELECT * FROM #Names
SELECT * FROM #phone|||thanks but in this case, I'm not able to send the IDENTITY to another stored procedure because I need to run through another stored proc to insert checkboxlist items and must do this through another call where I loop through each item in the checkbox. I suppose it would be much better to do what you say and just loop through the checkboxlist items first then pass one string to the same stored procedure...actually I'll try that way instead this time since I do know how to pass the identity or other parameters to other stored procs from within the same stored proc|||

Create proc ( int @.id Output) AS

Select * from table ;

Select @.id=@.@.identity;

===============================

cmd.Parameters.Add(@.id,SqlDbType.Int32);

cmd.Parameters["@.id"].Direction=ParameterDirection.Output;

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

return (int)cmd.Parameters["@.id"].value;

============================

i am not fimiliar with english, may be missing spell but here is a idea which i can work through out.

Wednesday, March 7, 2012

God Only Knows Why???

CREATE TABLE [__1] (
[n] [int] IDENTITY (1, 1) NOT NULL ,
[a] [decimal](16, 5) NULL
) ON [PRIMARY]
GO

INSERT INTO __1
(a)
VALUES (1.22115)

select * from __1

------------
RESULTS FROM Query Analyzer
------------

(1 row(s) affected)

n a
---- ------
1.00 1.22

(1 row(s) affected)

When I go to the Enterprise Manager and see the data, it appears as

n a
---- ------
1.00 1.22115

I am soooo confused!! Highly appreciating your comments...try inserting 1.22000|||Originally posted by RaedT
CREATE TABLE [__1] (
[n] [int] IDENTITY (1, 1) NOT NULL ,
[a] [decimal](16, 5) NULL
) ON [PRIMARY]
GO

INSERT INTO __1
(a)
VALUES (1.22115)

select * from __1

------------
RESULTS FROM Query Analyzer
------------

(1 row(s) affected)

n a
---- ------
1.00 1.22

(1 row(s) affected)

When I go to the Enterprise Manager and see the data, it appears as

n a
---- ------
1.00 1.22115

I am soooo confused!! Highly appreciating your comments...
it gives right results for me.|||Karolyn:
After iserting 1.22000, nothing changed!
the output is still rounded to 1.22

harshal_in:
Notice the output from the Query Analyzer. The inserted value 1.22115 was rounded to 1.22, I need the Query Analyzer to give me what really exists in the table 1.22115, not 1.22!!

Originally posted by harshal_in
it gives right results for me.|||sorry I've mis-read your pb

it's the same pb as last week !

you must have a property or option in your QA that
rounds up your data

or maybe you don't see it totally... (?)

try

Select '-' + convert(varchar(50),a) + '-' From Table|||CREATE TABLE [__1] (
[n] [int] IDENTITY (1, 1) NOT NULL ,
[a] [decimal](16, 5) NULL
) ON [PRIMARY]
GO

INSERT INTO __1
(a)
VALUES (1.22115)

select 'Value = '+convert(varchar,a) from __1

What does this return ?|||Output:
------------
Value = 1.22115

? What does this mean?

Originally posted by Enigma

CREATE TABLE [__1] (
[n] [int] IDENTITY (1, 1) NOT NULL ,
[a] [decimal](16, 5) NULL
) ON [PRIMARY]
GO

INSERT INTO __1
(a)
VALUES (1.22115)

select 'Value = '+convert(varchar,a) from __1

What does this return ?|||that you don't see the all the numbers of your result

the column in the QA is too narrow
maybe there's an option to adapt the witdht of a column|||Just curious .. whats the version of sql query analyzer are you using ...
I found no option in QA that does rounding off :confused:|||Go see TOOLS-OPTIONS in the QA

in the Result tab there's a number of caracters

(maybe someone that doen't like you set it to 4)|||QA Version: 8.00.760
Max. Characters per column: 256

Note, even the int type value (1) appears as 1.00

Originally posted by Karolyn
Go see TOOLS-OPTIONS in the QA

in the Result tab there's a number of caracters

(maybe someone that doen't like you set it to 4)|||At least you've got the good result in the table...
(Being optimistic)

Check all the options in your databases and in QA|||how are you starting up your query analyzer ?

By clicking on an icon ? The only thing i can think of is that you might be starting up your qa with some configuration options

Check your shortcut properties ... the error might lie there|||I am starting my QA from the EM.

TOOLS=> SQL QA

Originally posted by Enigma
how are you starting up your query analyzer ?

By clicking on an icon ? The only thing i can think of is that you might be starting up your qa with some configuration options

Check your shortcut properties ... the error might lie there|||did you find the round-up-property ??|||I can go ahead of my work, I converted the decimal numbers to varchar, so I can get the results with 5 decimal places, but I am so frustrated and need to know why? I am so curious to know what the problem is.

Originally posted by Karolyn
did you find the round-up-property ??|||If you see BlindMan or Breitt Kaiser online
Ask them !!!|||I get

n a
---- ------
1 1.22115

(1 row(s) affected)

No way an int is decimal(5,2)

I checked the options for the result set and don't see anything...

What collation are you using...(doubt that that's it either)

I don't buy that a column defined like you posted will ever display an identity that way...|||Grasping at straws here, but Karolyn obligates me to respond...

What is your "Use regional settings when outputting currency, number, dates, and times" setting in QA options? (It's in the connections tab.)

If it is on, try setting it to off.

From Books Online:

"Use regional settings when outputting currency, number, dates, and times.
Turning this setting to ON causes the ODBC driver to respect the local client setting when converting numeric, date, time, and currency values to character strings. The conversion is from SQL Server native data types to character strings only. When the setting is OFF, the driver does not convert numeric, date, time, and currency data to character string data using the client locale setting. The conversion setting is only applicable to output conversion and is only visible when currency, numeric, date, or time values are converted to character strings (which is always the case with Query Analyzer). The default for this setting is OFF. "|||It's in

Tools>Options

Menu

Never even thought to look their...

if that's the case...

Why do people mess with ANY settings...I leave'em alone...

God, If I had to remeber what I set...I'd go crazy....

Certainly ups the Mriacle ante.....

as in "God only knows why"|||Blindman .. i believe you have hit the nail on the head ...|||If I did, it's only because it was the last thing that could be suggested!|||I never would have found that in QA. Good job, Blindman.|||Blindman, you deserve millions kisses. You are a superman.
Brett Kaiser: I did not get all what you posted, but I can smell the aggressive way in your reply. Sorry for INCONVENIENCE and thanks anyway.
Thank you all for help, now I can sleep well.|||Originally posted by RaedT
Blindman, you deserve millions kisses. You are a superman.
Brett Kaiser: I did not get all what you posted, but I can smell the aggressive way in your reply. Sorry for INCONVENIENCE and thanks anyway.
Thank you all for help, now I can sleep well.

Whatever...It was a stretch for us to figure out what happend...know why?

Because we never touch that stuff...

But it seems like someone did...why?

My point was exactly that...don't mess with the settings...|||still that agressive tone in your writing...|||I can't believe I got that one. Like I said, you guys had already eliminated everything else.|||Brett Kaiser: Man take a chill pill,
That option is set to ON by default after installation.

Originally posted by Brett Kaiser
Whatever...It was a stretch for us to figure out what happend...know why?

Because we never touch that stuff...

But it seems like someone did...why?

My point was exactly that...don't mess with the settings...|||"That option is set to ON by default after installation."

Really? I don't recall seeing that set to ON before. And I don't recall every changing it.|||RaedT
try and see if there's someone in your entourage
that doesn't like you
and put that option to ON|||That's because it's not....|||If setting that value to ON is somebody's idea of being malicious, they aren't very imaginative. It was probably set by accident, or there was a "reason for it at the time".

Sunday, February 26, 2012

global vars in script files ?

Is it possible to declare a global variable in a SQL script ... or some
workaround ?
[ContentDB].[dbo].[PageTypes].[ptId] IDENTITY(int, 1,1)
In the following script, the local var @.ptId is lost once a "GO" is
executed.
USE [ContentDB]
GO
INSERT INTO [dbo].[PageTypes]
([ptName]
,[ptPath]
,[ptParamName])
VALUES
('unused'
,'/redirect.aspx'
,'url')
DECLARE @.ptId int
SET @.ptId = @.@.IDENTITY
.
.
.
.
<lots and lots of other SQL>
.
.
.
.
GO
.
.
.
.
<lots and lots of other SQL>
.
.
.There are no global variables in TSQL. You can use a temp table for this, or
check out SET
CONTEXT_INFO.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:eYtWs8JRGHA.1688@.TK2MSFTNGP11.phx.gbl...
> Is it possible to declare a global variable in a SQL script ... or some wo
rkaround ?
> [ContentDB].[dbo].[PageTypes].[ptId] IDENTITY(int, 1,1)
> In the following script, the local var @.ptId is lost once a "GO" is execut
ed.
> USE [ContentDB]
> GO
> INSERT INTO [dbo].[PageTypes]
> ([ptName]
> ,[ptPath]
> ,[ptParamName])
> VALUES
> ('unused'
> ,'/redirect.aspx'
> ,'url')
> DECLARE @.ptId int
> SET @.ptId = @.@.IDENTITY
> .
> .
> .
> .
> <lots and lots of other SQL>
> .
> .
> .
> .
> GO
> .
> .
> .
> .
> <lots and lots of other SQL>
> .
> .
> .
>

Sunday, February 19, 2012

Giving Value for Identity column while inserting

I have a table with an Identity column. The latest Identity value is 1298. A row with Identity 324 was deleted by mistake and I need to insert it now. But if I insert it now it will have Identity value of 1299. I tried giving the value 324 for the Identity column in the Insert statement but it throws error. How can I insert this row with the same Identity value as before??

Quote:

Originally Posted by sajithamol

I have a table with an Identity column. The latest Identity value is 1298. A row with Identity 324 was deleted by mistake and I need to insert it now. But if I insert it now it will have Identity value of 1299. I tried giving the value 324 for the Identity column in the Insert statement but it throws error. How can I insert this row with the same Identity value as before??


Are you not able to insert using a plain insert statement on query analyser?|||You can do this by
SET IDENTITY_INSERT [table_name] ON
and then turn it off