Stored Prcedure And @@indentity
Jul 12, 2005
I'm trying something new, and have hit a snag.
basically, I'm attempting to use two INSERT commands in a single SP. I want to use the PK from the new entry and apply that to the second.
here's the code;
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::
CREATE PROCEDURE spInsertNewUser
@fn varchar(50),
@ln varchar(50),
@add varchar(50),
@add2 varchar(50),
@ct varchar(50),
@cty varchar(50),
@st varchar(4),
@pc varchar(50),
@ph varchar(50),
@cp varchar(50),
@fx varchar(50),
@em varchar(50),
@pw varchar(50),
@uid varchar(50),
@rc bit,
--@indentity int OUTPUT,
@cid int
AS
SET NOCOUNT ON
INSERT INTO tblUserInfo
(
firstName,
lastName,
address,
address2,
city,
county,
state,
postalCode,
phone,
cellPhone,
fax,
email,
pword,
userUID,
regConfirmed
)
VALUES (
@fn,
@ln,
@add,
@add2,
@ct,
@cty,
@st,
@pc,
@ph,
@cp,
@fx,
@em,
@pw,
@uid,
@rc
)
SELECT @@IDENTITY FROM tblUserInfo
--SET @indentity = @@IDENTITY
INSERT INTO tblUserToCountySubscribedLKP (
UserID, CountyID
) VALUES(
@@indentity, @cid
)
:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::
I'm sure the problem is largely due to the fact that I am close to completely clueless on such things, but a bit of timely advice from the sage sql wizards here would be most welcome and greatly appreciated.
View 5 Replies
ADVERTISEMENT
Aug 3, 2007
hi focks;
through the jobs is possible it s working fine but
is it possible to run the packages through the stired procedure 2005
ok if possible how please help me
regards
koti
View 1 Replies
View Related
Oct 18, 2007
Hello.
I have a SQL table with one column that has the type = image.
I have a Stored procedure that inserts an image into that table.
CREATE PROCEDURE [dbo].[Test]
@test image
AS
BEGIN
insert into Table_1(test)values(@test)
END
I am using ADO in C++ for creating a connection,command, and parameters.
Code Block
VARIANT par;
par.vt = VT_ARRAY;
par.pbVal = (BYTE*)pBuffer;
pCommand->Parameters->Append(pCommand->CreateParameter("test",DataTypeEnum::adArray, ParameterDirectionEnum::adParamInput,strlen(pBuffer),par));
pCommand->Execute(NULL,NULL,CommandTypeEnum::adCmdStoredProc);
where
Code Block
_CommandPtr pCommand;
pCommand->CommandText = (_bstr_t)procedureName;
pCommand->CommandType = CommandTypeEnum::adCmdStoredProc;
//When I append the created parameter, in comutil.h, at these lines :
namespace _com_util {
inline void CheckError(HRESULT hr) throw(...)
{
if (FAILED(hr)) {
_com_issue_error(hr);
}
}
}
I get an exception : m_hresult 0x80020008 Bad variable type.
If I try to insert a string or an number, it works. But with BYTE* it dosen't.
Can someone help me?
Thanks
View 7 Replies
View Related
Jul 20, 2005
Hi All,I want to catch the next MSSQL error in my SQL code with following continuecalculationsServer: Msg 17, Level 16, State 1, Line 1SQL Server does not exist or access denied.If REMOTE_SERVER_1 is inaccessible (as in (a) below) the executing of SQLwill not continue with (b) - I need the code in (b) to run despite whetherthe previous exec was successful or not - Any ideas?begin transaction(a) exec REMOTE_SERVER_1...bankinsert '1' , '1' , 1 , 0 , 0(b) print @@errorcommit transactionwhere REMOTE_SERVER_1 is link to server created byEXEC sp_addlinkedserver @server = 'REMOTE_SERVER_1', @srvproduct = '',@provider = 'SQLOLEDB', @datasrc = 'MYCOMP1', @catalog = 'mirror2'EXEC sp_addlinkedsrvlogin @rmtsrvname = 'REMOTE_SERVER_1', .....Exec sp_serveroption 'REMOTE_SERVER_1', 'data access', 'true'Exec sp_serveroption 'REMOTE_SERVER_1', 'rpc', 'true'Exec sp_serveroption 'REMOTE_SERVER_1', 'rpc out', 'true'Exec sp_serveroption 'REMOTE_SERVER_1', 'collation compatible', 'true'Any help will be greatly appreciated
View 1 Replies
View Related
Mar 16, 2007
Hi everyone.
What is meant by stored procedure?
DECLARE @newyear int ]
set @newyear = 2007 ]----> is it a stored procedure?
what about the CSVTable? is CSVTable a stored procedure?
Thanks. Im just confused.
-Ron-
View 3 Replies
View Related
Feb 11, 2007
Hi there,
I am creating a report from a stored procedure that returns
multiples record sets. For an example, my stored procedure is as follows
CREATE PROCEDURE MYPROCEDURE
AS
BEGIN
SELECT * FROM TABLE1
SELECT * FROM TABLE2
SELECT * FROM TABLE3
END
Now lets say I want to create a report that will display the
result of the query no 2 (which is SELECT * FROM TABLE2 in this example). How
can I do this? Is there any way to bind the second record set to the report
dataset with out changing the stored procedure?
I will appreciate any kind of suggestions on this
Thanks
Moim
View 8 Replies
View Related
Feb 20, 2006
Arkadiy writes "Why @@indentity returns NULL after inserting new row?"
View 6 Replies
View Related
Jun 15, 2000
Hi folks!
I need to populate a table having an identity field with data (basically I want to make a copy of a table with identity column on another server). When I use BCP to insert data then the values in the identity field is not the same as in the data file but gets incremented from the original.
eg. if the records in the flat file are-
1, 'test1'
2, 'test2'
Now if i insert this into the table (say the last identity value was 100) then rows get inserted as-
101, 'test1'
102, 'test2'
Is there any way by which I can retain the values of the identity column to be same as in the file?
Thanks!
View 1 Replies
View Related
Feb 24, 2000
when I use the code
SET IDENTITY_INSERT tss_Cable ON
go
DECLARE @@nextidentval int
SELECT @@nextidentval = MIN(IDENTITYCOL) + IDENT_INCR(tss_Cable)
FROM tss_Cable t1
WHERE IDENTITYCOL BETWEEN IDENT_SEED(tss_Cable) AND 2147483646
AND NOT EXISTS (SELECT * FROM tss_Cable t2
WHERE t2.IDENTITYCOL = t1.IDENTITYCOL IDENT_INCR(tss_Cable))
go
SET IDENTITY_INSERT tss_Cable OFF
I get the following error.
error line 2 Incorrect syntax near IDENT_INCR.
error line 6 incorrent syntax near IDENT_INCR.
I would like to reset the identity without droping the table. I'm using
sql 6.5. I looked at Mark Lessard 12/17/99 respose on this issue and changed
the code to match his version but the same error occured.
View 1 Replies
View Related
Apr 7, 2003
How do reset identity seed on table, for example I have table that has gap in the identity such as (1,2,3,5,6, etc..) and I want reset the entire table.
Thank You,
John
View 2 Replies
View Related
Feb 27, 2006
What is the best way to do Indentity(1,1) on fields which is varchar(8)?
View 1 Replies
View Related
Jul 20, 2005
I'm doing a data transfer from Access to SQL Server, I wish to keep theidentity column (autonumber) values as all the data is already related. Itried the first table append query including the identity column, it worked.Was this fluke? Will it always work? I was under the impression that I wouldhave to issue a "set identity_insert on" before doing this. The SQL databasewill have absolutely no data before the transfer routines are run.
View 15 Replies
View Related
Apr 25, 2008
IDE: Visual Studio 2008
DB: SSCE 3.5
OS: WinXP SP2 (CHS)
.....
Code Snippet
String str_sql_insert = "INSERT INTO Person (Name, ShortName, Description, Tag) VALUES ('" + this.TextBox.Text.Trim() + "', '" + this.ShortNameTextBox.Text.Trim() + "', '" + this.DescriptionTextBox.Text.Trim() + "','" + this.TagComboBox.SelectedValue.ToString().Trim() + "); SELECT @@INDENTITY";
SqlCeConnection tempConn = dbman.getConn();
SqlCeCommand tempComm = new SqlCeCommand(str_sql_insert, tempConn);
tempConn.Open();
Object ob = tempComm.ExecuteScalar();
.....
When I run the application there is a error report on the last line of the code above:
[ Token line number = 1,Token line offset = 127,Token in error = SELECT ]
When I copy the SQL string into the QueryBuilder in the VS2008 it is works and returned a correct value.
I want to know is there something wrong with the sql string or the ExecuteScalar() cant execute 2 steps command?
thanks
View 6 Replies
View Related
Feb 22, 2008
As an example I have three tables
Table1
ChildName (nvarchar)30
ChildID (INT) Set as €˜IDENTITY€™
Table2
ParentName(nvarchar)30
ParentID (INT) Set as €˜IDENTITY€™
Table3
ChildID (INT)
ParentID (INT)
I have the following code.
Dim dbcnn As SqlClient.SqlConnection = New SqlClient.SqlConnection(dbsettings.getDatabaseString)
Dim dbcmd As SqlClient.SqlCommand
dbcmd_AddDriverRoute = New SqlClient.SqlCommand("usp_Insert", dbcnn)
dbcmd.CommandType = CommandType.StoredProcedure
dbcmd.Parameters.AddWithValue("@ChildID ", cmb_child.SelectedItem)
dbcmd.Parameters.AddWithValue("@ParentID ", "@@IDENTITY")
dbcnn.Open()
dbcmd.ExecuteScalar()
dbcnn.Close()
basically what I want to do is when a child is added I want to be able to insert ParentID (INT) Set as €˜IDENTITY€™ value into table3 ParentID (INT) .
I have my sql statement in place I am not sure how to take an identity value stored in the db.
View 6 Replies
View Related
Mar 24, 2008
i have table with one column
CREATE TABLE [dbo].[tblTest](
[ID] [int] IDENTITY(1,1) NOT NULL
how do i insert values.(i,e identity)
View 14 Replies
View Related
Nov 15, 1999
I would like to reset the Identity column in a table so it starts at the begining. I am planning to clean out this table.
Thanks
Gary
View 1 Replies
View Related
Jan 4, 2005
Hello folks,
I am using the following INSERT statement to add a new record in my table. I want to be able to get the Autonumber UserID field of the newly created record but I am getting the following error: "Characters found after end of SQL statement"
Code:
INSERT
INTO tblUser
(LoginName, UserPassword, EmailAddress, City)
Values
(@User, @UsrPassword, @EmailAddress, @City);
SELECT @@IDENTITY As 'Identity'
What is wrong in my statement?
How do I retrieve the Autonumber field of the newly created record?
How do I handle if the LoginName and/or EmailAddress already exists?
I want to be able save this INSERT statement as an Access Query and call it from my C# code.
I would appreciate any help.
Thanks
Mike
View 1 Replies
View Related
Mar 24, 2006
Hi Guys,
I'm using SQL server 2000.
How do I alter column/field from type int (with Identity = Yes Not For Replication) to just normail int field. No more identity. I want it to be done using SQL script( sql query analyzer).
Please help me on this, thx
Regards,
Shaffiq
View 4 Replies
View Related
Sep 11, 2006
I imported a table using DTS.
I run SQL statements in my original server database. It works well.
But when I run the same instructions within a stored procedure in my new workstation where I improrted the table I get this error:
Server: Msg 515, Level 16, State 2, Procedure InsertFichierPrix, Line 11
Cannot insert the value NULL into column 'Id', table 'myDBLive.dbo.FichierPrix'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Here is the SQL statements that I run on the original Database:
insert into fichierprix(nomfichier, version, descriptionfr, typeclient,....) values(@NomFichier,'Actuelle', @NomFourFr, 'MembreAcheteur', ....)
And here is my SP that I run on the new imported database:
CREATE PROCEDURE InsertFichierPrix @NomFichier varchar(50), @NomFr varchar(50),@NomAn varchar(50)
AS
declare @NomFourFr varchar(50)
declare @NomFourAn varchar(50)
SET @NomFourFr='liste fournisseur ' + @NomFr
set @NomFourAn='liste fournisseur ' + @NomAn
insert into fichierprix(nomfichier, version, descriptionfr, typeclient,...) values(@NomFichier,'Actuelle', @NomFourFr, 'MembreAcheteur'...)
GO
And here is the execution of my Stored proc on the destination database:
execute insertfichierprix @NomFichier='myfilename2', @NomFr='fournifr1',@NomAn='Fourniang1'
Thank you for helping me.
View 2 Replies
View Related
Jul 20, 2005
Hi,When i eg. manually ad entries to a table and, cancels the insert Ms SQLincrement the counter on the ID anyway. Is there a way to avoid thisbehavior?RegardsAnders
View 1 Replies
View Related
Jul 20, 2005
Hi AllI have a table in SQL Server with ID having indentity inrement by one.Table has not any trigger. Frequently ID in the table jumps.Any help !!!Thanks
View 2 Replies
View Related
Dec 6, 2007
I am trying to remove the Indentity property from a column so I can do some clean up. Then will need to add it back. Adding Indentity property back is not a problem, but cant figure out how to remove it in the first place.
Any help would be greatly apprecaited.
Thanks
Dave
View 4 Replies
View Related
Nov 16, 2004
Hello, everyone:
I have a table with an indentity column as first column. At beginning it is continue such as 0-50. I delete last 20 columns by hand. The 0-31 is left. When the new data is inserted, I hope the new indentity column begin from 32. How to do that? Now the indentity column begin from 51 as the new data is inserted.
Thanks a lot
ZYT
View 9 Replies
View Related
Aug 1, 2007
Hi There
Can anyone tell me how an identity column decides what number to give which row.
I always thought that it was in order of the clustered index.
But i have found the following:
If you have Table A with a clustered index(not unique) say on the first 3 columns.
If the table is empty and you add an identity column, then afterward load it with data the value of the identity column seems to match the clustered index.
But if the table has no identity column and it is filled with data, and then you add an identity column the number for the identity column seems to be all over the place.
So basically how does a newly added identity column know what number to give each row, what if the clustered index is not unique? Is it random ?
Thanx
View 4 Replies
View Related
Feb 20, 2008
Dear Friends,
I need a SQL Query to add a identity coloumn for anExisting table. (ie) when i try to alter the table i want to add an identity coloumn.
Thanks in advance.
View 8 Replies
View Related
Nov 1, 2007
Hi all - I'm trying to optimized my stored procedures to be a bit easier to maintain, and am sure this is possible, not am very unclear on the syntax to doing this correctly. For example, I have a simple stored procedure that takes a string as a parameter, and returns its resolved index that corresponds to a record in my database. ie
exec dbo.DeriveStatusID 'Created'
returns an int value as 1
(performed by "SELECT statusID FROM statusList WHERE statusName= 'Created')
but I also have a second stored procedure that needs to make reference to this procedure first, in order to resolve an id - ie:
exec dbo.AddProduct_Insert 'widget1'
which currently performs:SET @statusID = (SELECT statusID FROM statusList WHERE statusName='Created')INSERT INTO Products (productname, statusID) VALUES (''widget1', @statusID)
I want to simply the insert to perform (in one sproc):
SET @statusID = EXEC deriveStatusID ('Created')INSERT INTO Products (productname, statusID) VALUES (''widget1', @statusID)
This works fine if I call this stored procedure in code first, then pass it to the second stored procedure, but NOT if it is reference in the second stored procedure directly (I end up with an empty value for @statusID in this example).
My actual "Insert" stored procedures are far more complicated, but I am working towards lightening the business logic in my application ( it shouldn't have to pre-vet the data prior to executing a valid insert).
Hopefully this makes some sense - it doesn't seem right to me that this is impossible, and am fairly sure I'm just missing some simple syntax - can anyone assist?
View 1 Replies
View Related
Apr 29, 2008
How do I search for and print all stored procedure names in a particular database? I can use the following query to search and print out all table names in a database. I just need to figure out how to modify the code below to search for stored procedure names. Can anyone help me out?
SELECT TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
View 1 Replies
View Related
Oct 14, 2007
I am calling a stored procedure (say X) and from that stored procedure (i mean X) i want to call another stored procedure (say Y)asynchoronoulsy. Once stored procedure X is completed then i want to return execution to main program. In background, Stored procedure Y will contiue his work. Please let me know how to do that using SQL Server 2000 and ASP.NET 2.
View 3 Replies
View Related
Nov 5, 2015
Can I invoke stored procedure stored inside from a user defined table column?
View 5 Replies
View Related
Jun 13, 2007
Seems like I'm stealing all the threads here, : But I need to learn :) I have a StoredProcedure that needs to return values that other StoredProcedures return.Rather than have my DataAccess layer access the DB multiple times, I would like to call One stored Procedure, and have that stored procedure call the others to get the information I need. I think this way would be more efficient than accessing the DB multiple times. One of my SP is:SELECT I.ItemDetailID, I.ItemDetailStatusID, I.ItemDetailTypeID, I.Archived, I.Expired, I.ExpireDate, I.Deleted, S.Name AS 'StatusName', S.ItemDetailStatusID, S.InProgress as 'StatusInProgress', S.Color AS 'StatusColor',T.[Name] AS 'TypeName', T.Prefix, T.Name AS 'ItemDetailTypeName', T.ItemDetailTypeID FROM [Item].ItemDetails I INNER JOIN Item.ItemDetailStatus S ON I.ItemDetailStatusID = S.ItemDetailStatusID INNER JOIN [Item].ItemDetailTypes T ON I.ItemDetailTypeID = T.ItemDetailTypeID However, I already have StoredProcedures that return the exact same data from the ItemDetailStatus table and ItemDetailTypes table.Would it be better to do it above, and have more code to change when a new column/field is added, or more checks, or do something like:(This is not propper SQL) SELECT I.ItemDetailID, I.ItemDetailStatusID, I.ItemDetailTypeID, I.Archived, I.Expired, I.ExpireDate, I.Deleted, EXEC [Item].ItemDetailStatusInfo I.ItemDetailStatusID, EXEC [Item].ItemDetailTypeInfo I.ItemDetailTypeID FROM [Item].ItemDetails IOr something like that... Any thoughts?
View 3 Replies
View Related
Mar 27, 2006
Hello to all!
I have a table name stored in a scalar variable (input parameter of my stored procedure). I need to run SQL statement: SELECT COUNT (*) FROM MyTable and store the result of my query in a scalar variable:
For example:
declare @countRows int
set @countRows = (select count(*) from MyTable)
The problem is that the name of MyTable is stored in the input variable of my stored procedure and of corse this does not work:
declare @countRows int
set @countRows = (select count(*) from @myTableName)
I also tried this:
declare @sqlQuery varchar(100)
set @sqlQuery = 'select count(*) from ' + @myTableName
set @countRows = exec(@sqlQuery)
But it looks like function exec() does not return any value...
Any idea how to solve this problem?
Thanx,
Ziga
View 3 Replies
View Related
May 13, 2008
Greetings:
I have MSSQL 2005. On earlier versions of MSSQL saving a stored procedure wasn't a confusing action. However, every time I try to save my completed stored procedure (parsed successfully ) I'm prompted to save it as a query on the hard drive.
How do I cause the 'Save' action to add the new stored procedure to my database's list of stored procedures?
Thanks!
View 5 Replies
View Related
Apr 7, 2006
We recently upgraded to SQL Server 2005. We had several stored procedures in the master database and, rather than completely rewriting a lot of code, we just recreated these stored procedures in the new master database.
For some reason, some of these stored procedures are getting stored as "System Stored Procedures" rather than just as "Stored Procedures". Queries to sys.Objects and sys.Procedures shows that these procs are being saved with the is_ms_shipped field set to 1, even though they obviously were not shipped with the product.
I can't update the sys.Objects or sys.Procedures views in 2005.
What effect will this flag (is_ms_shipped = 1) have on my stored procedures?
Can I move these out of "System Stored Procedures" and into "Stored Procedures"?
Thanks!
View 24 Replies
View Related