Showing posts with label second. Show all posts
Showing posts with label second. 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.

Friday, March 9, 2012

Good design?

Hi!

I want to add "inheritance" to my database. I have of table X witch attributes a1, a2, a3, a4, a5, a6 and second table Y with attributes a1, a2, a3, a7.

I want to create additional table Z with attributes 1a, a2, a3, key and tables X: key, a4, a5, a6 and table Y: key, a7.

I have more of those sub-class. Set of common attributes is about 5-10. Additional attributes range from 1 to 20 per table.

We have also large dynamics of our database. Often (1 per month) we have to add about 2-5 attributes to some subclass (or remove some attributes).

It's good idea for "attributes inheritance"?

I know those disadvantages:

to insert (update, delete) record from X, I have to insert record to Z and X

I found solution that told me to add all attributes to one table. It's not good for me. Storage space is not optimal. I don't want table with 100-200 columns in majority filled with null's.

Any better ideas?

Best Regards,
Walter

>>I found solution that told me to add all attributes to one table. It's not good for me. Storage space is not optimal. I don't want table with 100-200 columns in majority filled with null's.<<

Storage wouldn't be a problem, as pretty much the same amount of space is taken up. But it is really ugly.

>>It's good idea for "attributes inheritance"?<<

It really and truly depends. If you really have a supertype that is useful to have that has the attributes that are the same AND the supertype models the same thing. Like an vehicle for tracking, then a car and a motorcycle because they have different attributes that the client needs, it is a great thing.

If you just have disparate things being modeled that happen to have some of the same attributes (like most tables I create have a name column, and update date, etc) then it is a bad idea and not necessary.

>>We have also large dynamics of our database. Often (1 per month) we have to add about 2-5 attributes to some subclass (or remove some attributes).<<

This sounds like a bad idea in principle, but it may not be. But when people say they add new columns too often it raises the red flag that says that the problem isn't quite understood. Most relational databses should be quite static with the data they store if the problem that is being modeled is understood. But there are obvious exceptions, and yours might be it :)

|||

Hi!

Yes you've right, there is not problem in storage, but 50-70 columns per table is not good ...

In fact I design database of bialiffs, offices, courts, etc. for large lawyer's office. Those institutions have many common attributes like phone, name, etc. Altough some of them have more or less additional parameters. Next... offices divides on tax offices, registry offices, province offices, an so on... But they all are institutions.

In previous design we have unique id for bailiffs, courts and offices. All kinds of offfices was in same table. The problem arose, when we have to build list of debt collectors in our other soft... Naturally it is bailiffID. But some of tax offices can be debt collectors also. So we have to store not only ID, but context also (tax office, bailiff, etc.). In new design we have to point only on InstitutionID.

Our system must be dynamic! :) Yes, we can describe every institution, but our friendly lawyer's office doesn't need it! They don't need 200 attributes of tax office, but only 10-15 witch are necessary. Our often changes follows on:

* changes in our Polish law - unfortunately system is not stable today ... :( But isn't bad ... :)
* In next iteration of our project we transtorms next "pencil and paper" procesess into software. We must be "agile" :)

Best Regards,
Walter

|||

>>Yes you've right, there is not problem in storage, but 50-70 columns per table is not good ...<<

Taken out of context, I wouldn't agree, but if they don't all need the columns then I wholeheartedly agree.

>> previous design we have unique id for bailiffs, courts and offices. All kinds of offfices was in same table. The problem arose, when we have to build list of debt collectors in our other soft... Naturally it is bailiffID. But some of tax offices can be debt collectors also. So we have to store not only ID, but context also (tax office, bailiff, etc.). In new design we have to point only on InstitutionID.<<

I like the idea. This is a fantastic use of a subtype, to be sure.

>>Our system must be dynamic! :) Yes, we can describe every institution, but our friendly lawyer's office doesn't need it! They don't need 200 attributes of tax office, but only 10-15 witch are necessary. Our often changes follows on:<<

I fear the word dynamic because it tends to mean overly flexible. Flexibility is for the UI guys. Data that is important enough to design a database for is important enough to design right (not that you have said anything that leads me to believe any other way,) and protect. I will trade a bit of hard work and complexity any day for a overly flexible, too hard to manage system tomorrow. I work hard now so "future me" can cruise.

>>* changes in our Polish law - unfortunately system is not stable today ... :( But isn't bad ... :)
* In next iteration of our project we transtorms next "pencil and paper" procesess into software. We must be "agile" :)<<

Don't confuse agile with overly flexible because no one will confuse overly flexible with failure. Agile is good, but build the foundation of your systems on solid rock and the rest will come easy. :)

Sunday, February 19, 2012

GK - Persmission on each database ( objects )

Hi,
Second attempt to post this same message ( yesterdays post is not to be foud
in this forum ? ).
I like to retrieve ALL permissions on all abjects for each user ( each role,
each login ) in ALL of the databases within one server environment. I tried
the standard "sp_" procs but they are to limited for this.
I have to deal with approx 250 databases on approx 60 servers so I like to h
ave something which I can use on server level and not on table level ( like
the standard procs ).
As an Oracle DBA I'm not that familiar with scripting on SQL server 2000 so
l need your help on this one.
who can help me out?
Thanks in advance,
Regards, GKramer
The netherlands.Hi,
Have a look into syspermissions system table, which stores all the
previleges granted using GRANT statement.
Thanks
Hari
MCDBA
"GKramer" <anonymous@.discussions.microsoft.com> wrote in message
news:D6C88E0C-8C67-4DAE-AB3E-9CBCD061AA28@.microsoft.com...
> Hi,
> Second attempt to post this same message ( yesterdays post is not to be
foud in this forum ? ).
> I like to retrieve ALL permissions on all abjects for each user ( each
role, each login ) in ALL of the databases within one server environment. I
tried the standard "sp_" procs but they are to limited for this.
> I have to deal with approx 250 databases on approx 60 servers so I like to
have something which I can use on server level and not on table level ( like
the standard procs ).
> As an Oracle DBA I'm not that familiar with scripting on SQL server 2000
so l need your help on this one.
> who can help me out?
> Thanks in advance,
> Regards, GKramer
> The netherlands.
>|||Hi,
Have a look into syspermissions system table, which stores all the
previleges granted using GRANT statement.
Thanks
Hari
MCDBA
"GKramer" <anonymous@.discussions.microsoft.com> wrote in message
news:D6C88E0C-8C67-4DAE-AB3E-9CBCD061AA28@.microsoft.com...
> Hi,
> Second attempt to post this same message ( yesterdays post is not to be
foud in this forum ? ).
> I like to retrieve ALL permissions on all abjects for each user ( each
role, each login ) in ALL of the databases within one server environment. I
tried the standard "sp_" procs but they are to limited for this.
> I have to deal with approx 250 databases on approx 60 servers so I like to
have something which I can use on server level and not on table level ( like
the standard procs ).
> As an Oracle DBA I'm not that familiar with scripting on SQL server 2000
so l need your help on this one.
> who can help me out?
> Thanks in advance,
> Regards, GKramer
> The netherlands.
>|||Hari,
Thanks for your quick response, but where do the id's refer to ? ( Where c
an I find the ERD according to the sys-tables )
1 id int 4 0
0 grantee smallint 2 0
0 grantor smallint 2 0
0 actadd smallint 2 0
0 actmod smallint 2 0
Guus Kramer|||Hi,
Details will be there in spt_values table in Master database.
0 - means it is Public
For Grantor and Grantee execute along with user_name function.
user_name(grantee),user_name(grantor)
For actadd and actmode join it with spt_values table.
Thanks
Hari
MCDBA
"GKramer" <anonymous@.discussions.microsoft.com> wrote in message
news:96235761-C242-4D52-8FAE-22E3B6C8A424@.microsoft.com...
> Hari,
> Thanks for your quick response, but where do the id's refer to ? ( Where
can I find the ERD according to the sys-tables )
> 1 id int 4 0
> 0 grantee smallint 2 0
> 0 grantor smallint 2 0
> 0 actadd smallint 2 0
> 0 actmod smallint 2 0
> Guus Kramer|||Look at sp_helprotect and the PERMISSIONS function also.
Cindy Gross, MCDBA, MCSE
http://cindygross.tripod.com
This posting is provided "AS IS" with no warranties, and confers no rights.|||Cindy,
As I told the forum the SP_ proc are way to limited. I need something wihich
generates an output like this;
database -- login -- (connected to ) role -- object(s) -- object(s) permissi
on
I'm not familiar with scripting MS sql ( I'm a former Oracle DBA and 3 month
s on the (SQLserver) job now ) and I can not find any documentation of how t
he systables are related ( ERD ).
Please help me on this because I have to examin 300 database on 60 server!!
Best regards,
Guus Kramer,
The Netherlands