SQL Server - how to add multiple record using Store procedure

Asked By vash i
09-Feb-12 02:05 AM

my current SP

ALTER PROCEDURE [dbo].[sp_Progress]

@MeetingID as int

as

DECLARE

 

@ProjectID as INT

SET @ProjectID=( SELECT ID FROM [Project])

 

INSERT INTO [Progress] (MeetingID, ProjectID)

VALUES (@projectID, @MeetingID)


error come out

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

because of project table have multiple value

 

 

  Somesh Yadav replied to vash i
09-Feb-12 02:12 AM

Hi

Many a times we have to insert multiple data on click of one button. Like inserting all the checked records in the favorite table of the member. It is not a good idea to insert the records by using as many insert queries.

At these points we should insert all the records with only one call to the database. We can use a stored procedure that takes a comma (or any separator you want to use) list of values and insert them in the database. This way the whole process will be very fast and efficient.

In this example we will insert data in the MemberProductfaveriote table. We will be insert the member ID and the MemberProductId(all the product selected as favorite from the list). We will pass the MemberID and the comma separated list of MemberProductId.

The stored procedure will insert each of the record in the database.

In the stored procedure we also check if the record is already inserted in the database. If the record in already present in the database we will not insert the record. We will only insert the record if the record in not present in the database. Here is the stored procedure



CREATE PROCEDURE dbo.insertMemberProductFavorite

    (

        @MemberId int,

        @MemberProductId varchar(100)

    )

AS

    DECLARE @count int

    DECLARE @str VARCHAR(8000)

    DECLARE @spot SMALLINT

 

    WHILE @MemberProductId <> ''

    BEGIN

      SET @spot = CHARINDEX(',', @MemberProductId)

      IF @spot>0

        BEGIN

          SET @str = CAST(LEFT(@MemberProductId, @spot-1) AS INT)

          SET @MemberProductId = RIGHT(@MemberProductId, LEN(@MemberProductId)-@spot)

        END

      ELSE

        BEGIN

          SET @str = CAST(@MemberProductId AS INT)

          SET @MemberProductId = ''

        END

      

   SELECT @count=count(1) FROM MemberProductFavorite

   WHERE MemberProductId=@str

   AND MemberId=@MemberId

  

   IF @count =0

   BEGIN

    INSERT INTO MemberProductFavorite

                        (MemberId,

                        MemberProductId)

              VALUES

                    (    @MemberId,

                        @str)

   END

    END

    RETURN

GO

Hope this helps
Thanks

  vash i replied to Somesh Yadav
09-Feb-12 02:17 AM
already found this article..but dont know how to apply in my case..need to understand 1st lol..
anyway thanks 4 ur help..
  Dilip Sharma replied to vash i
09-Feb-12 09:30 AM


if ur query will return more then 1 id it will give error...

u can use  this ...


@ProjectID as INT

SET @ProjectID=( SELECT top 1 ID FROM [Project])

 

INSERT INTO [Progress] (MeetingID, ProjectID)

VALUES (@projectID, @MeetingID)




Create New Account
help
How can I decrypt a SQL Server stored-procedure? SQL Server hi Please help on this How can I decrypt a SQL Server stored-procedure? thx guru SQL Server Programming Discussions SQL Server (1) Stored procedure (1) Stu (1
Additonal thoughts, you should be able to connect to SQL Server via SQL Server SQL Server Additonal thoughts, you should be able to connect to SQL Server via SQL Server Management Studio. This will provide you with whether or not SQL Server
Can't login to SQL Server SQL Server Hello, I'm trying to migrate one SQL Server 2000 database to SQL Server Express, and I'm having the following issue. In the SQL Server 2000 database I
Alias for Linked SQL Server? SQL Server Hi, I'm trying to connect to a linked SQL Server 2008 from my local SQL = Server 2008. Everything seems to work fine so far but I'm having the following = problem
Accessing data on a server? SQL Server I have SQL Server developer edition installed on one computer and SQL Server Express Edition install on another computer. When I try to "Attach" a database located on my file server, SQL Server does not "see" the network drive. Is there anyway to get a local