sql server - Inserting values automatically in sql -


i have following stored procedure insert purchasing of vouchers, recieving user_id foreign key in voucher table, other fields entered automatically, here stored procedure`

create proc insertpurchasevouchers     (        @user_id int = null,        @amount decimal(18,2)      )          begin     set nocount on       if not exists ( select  *                            voucher                           [user_id] = @user_id)                     insert voucher                     (                       voucher_id,                       voucher_amount,                       voucher_expiritydate,                       voucher__purchasedate,                       [user_id]                     )                     values(                                1,                                @amount,                                dateadd(m,3,getdate()),                                getdate(),                                @user_id                           )                           set voucher_id  = scope_identity      end` 

and tables

 use [csis3714_makaringih] go  /****** object:  table [dbo].[voucher]    script date: 2015/05/14 11:11:09 ******/ set ansi_nulls on go  set quoted_identifier on go  create table [dbo].[voucher](     [voucher_id] [numeric](18, 0) not null,     [voucher_amount] [decimal](18, 2) null,     [voucher_expiritydate] [datetime] null,     [voucher__purchasedate] [datetime] null,     [user_id] [int] null, primary key clustered  (     [voucher_id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]  go  alter table [dbo].[voucher]  check add  constraint [fk_voucher_user] foreign key([user_id]) references [dbo].[user] ([user_id]) go  alter table [dbo].[voucher] check constraint [fk_voucher_user] go      use [csis3714_makaringih] go  /****** object:  table [dbo].[user]    script date: 2015/05/14 11:12:14 ******/ set ansi_nulls on go  set quoted_identifier on go  set ansi_padding on go  create table [dbo].[user](     [user_id] [int] not null,     [user_name] [varchar](50) null,     [user_password] [nvarchar](200) null,     [user_cellnumber] [varchar](10) null,     [user_isactive] [nchar](10) null,     [userrole_id] [varchar](1) null,     [user_firstname] [varchar](50) null,     [user_surnamr] [varchar](50) null,     [user_idnumber] [varchar](13) null,     [user_airtimebalance] [decimal](18, 2) null, primary key clustered  (     [user_id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]  go  set ansi_padding off go  alter table [dbo].[user]  check add  constraint [fk_user_role] foreign key([userrole_id]) references [dbo].[userrole] ([userrole_id]) go  alter table [dbo].[user] check constraint [fk_user_role] go 

the voucher expires in 3 months

looking @ code, seems wanting populate voucher_id automatically. best bet add identity property voucher_id column. auto generate value in column when insert.

create table [dbo].[voucher]( [voucher_id] [bigint] not null identity, -- change bigint datatype, mark identity [voucher_amount] [decimal](18, 2) null, [voucher_expiritydate] [datetime] null, [voucher__purchasedate] [datetime] null, [user_id] [int] null, primary key clustered  (     [voucher_id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go 

your insert statement exclude the voucher_id column:

insert dbo.voucher (voucher_amount, voucher_expiritydate, voucher__purchasedate, user_id) values (@amount, dateadd(m,3,getdate()), getdate(), @user_id) 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -