insert problem

Results 1 to 2 of 2

Thread: insert problem

  1. #1
    Reed Guest

    Default insert problem

    When I execute this stored procedure from Query Analyzer (I have not written the asp/vb script code yet because I want to make sure this works first) it works great if the user exists. But if I put in a hokey username, the first insert succeedes and the last fails. I&#039;m really new at this and I&#039;m wondering how I can keep the inserts from executing if the username fails.<BR><BR>Thanks<BR><BR>Stored Proc:<BR>---------------------------------------------<BR>CREATE PROCEDURE sp_Insert_Task <BR><BR>@username varchar(30),<BR>@desc varchar(100),<BR>@time char(10),<BR>@project_id char(10)<BR><BR><BR>AS<BR><BR>declare @user_id int<BR>select @user_id = [user_id] from tblUsers where username = @username<BR><BR> INSERT INTO tblTasks <BR> (description, <BR> entry_dt, <BR> time_spent, <BR> project_id) <BR> VALUES <BR> (@desc, <BR> getDate(), <BR> @time, <BR> @project_id)<BR><BR> declare @identity int<BR> set @identity = @@Identity<BR><BR> INSERT INTO tblUsers_Projects <BR> ([user_id], <BR> project_id, <BR> task_id) <BR> VALUES <BR> (@user_id, <BR> @project_id, <BR> @identity)<BR><BR> return(1)<BR>----------------------------------------------------<BR>feel free to suggest other things too if needs be.<BR><BR>Reed

  2. #2
    Join Date
    Dec 1969
    Los Angeles, CA

    Default Easy

    declare @user_id int<BR><BR>select @user_id = [user_id] from tblUsers where username = @username<BR>if @user_id is not null (assuming it will return Null. if you have it set up where it will return a empty string then you can do if @user_id &#060;&#062;&#039;&#039;<BR>begin<BR>INSERT INTO tblTasks <BR>(description, <BR>entry_dt, <BR>time_spent, <BR>project_id) <BR>VALUES <BR>(@desc, <BR>getDate(), <BR>@time, <BR>@project_id)<BR><BR>declare @identity int<BR>set @identity = @@Identity<BR><BR>INSERT INTO tblUsers_Projects <BR>([user_id], <BR>project_id, <BR>task_id) <BR>VALUES <BR>(@user_id, <BR>@project_id, <BR>@identity)<BR><BR>return(1)<BR>end

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts