sql - Inserting record from one column to another column in the same scope or statement -


i have stored procedure populates table: table indicated in code below has identity column primary key column.

i append primary key contain leading letters: example: abc123. not possible because primary key column int datatype.

so created additional column can insert appended primary key. works except have make new column null , using update statement.

something tells me there better way.

is there way can without using update after initial insert , have new column categoryid not null?

table code:

create table [dbo].[registration] (     [systemid]   int          identity (100035891, 1) not null,     [categoryid] char (13)    null,     [fname]      varchar (30) not null,     [lname]      varchar (30) not null,     [minit]      char (1)     null,     primary key clustered ([systemid] asc) ); 

stored procedure:

create procedure [dbo].[uspinsertregistration]  @fname varchar(30), @lname varchar(30), @minit char(1), @categoryid char(13), @systemid int output  begin set nocount on declare @errcode int  insert [dbo].[registration] ([fname],[lname],[minit])   values (@fname, @lname, @minit)  select @errcode = @@error, @systemid = scope_identity()  update [dbo].[registration] set categoryid = 'abc'+ cast(systemid char)  set nocount off return @errcode end 

finally table looks data:

enter image description here

thanks being contagious knowledge. :)

guy

my suggestion use computed column, you're trying introduces redundancy. see below:

http://msdn.microsoft.com/en-us/library/ms191250%28v=sql.105%29.aspx

alternately, make big enough contain guid, put guid column on insert, update afterwards.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -