Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
CREATE PROCEDURE UpdateCustomer
(
@CustomerID BIGINT
,@Name VARCHAR(40)
,@Address1 VARCHAR(20)
,@Address2 VARCHAR(20)
,@District VARCHAR(20)
,@State VARCHAR(20)
,@Country VARCHAR(20)
,@ZIPPIN VARCHAR(20)
,@Website VARCHAR(20)
,@email VARCHAR(20)
,@Telephone VARCHAR(12)
,@ContactPerson VARCHAR(20)
,@Mobile VARCHAR(12)
,@Reference_Name VARCHAR(20)
,@Naration VARCHAR(50)

)
AS
DECLARE @FIRSTNAME VARCHAR(20);
DECLARE @LASTNAME VARCHAR(20);


SELECT @FIRSTNAME=SELECT Item from dbo.SplitString(@Name,' ')where ItemIndex=0
SELECT @LASTNAME=SELECT Item from dbo.SplitString(@Name, ' ')where ItemIndex=1

UPDATE customers SET CustomerName=@FIRSTNAME,CustomerLName=@LASTNAME,Address1=@Address1,Address2=@Address2,District=@District,State=@State,Country=@Country,ZIPPIN=@ZIPPIN,Website=@Website,email=@email,Telephone=@Telephone,ContactPerson=@ContactPerson,Mobile=@Mobile,Reference_Name=@Reference_Name,Naration=@Naration WHERE CustomerID=@CustomerID

While gridbing i contactenate the first name and lastname as name but while i going for update i have to split the name in to last name and first name for that i used split function
SQL
SELECT Item from dbo.SplitString(@Name, ' ')where ItemIndex=0

but when i assaigning into arguments @FirstName it showing the error
SQL
SELECT @FIRSTNAME=SELECT Item from dbo.SplitString(@Name, ' ')where ItemIndex=0

any body have idea

i need to split the name parameter based on space and assaign in to last name and first name.

any body have any idea.
Posted
Updated 6-Mar-14 23:19pm
v2

Just change
SQL
SELECT @FIRSTNAME=SELECT Item from dbo.SplitString(@Name, ' ')where ItemIndex=0

to
SQL
SELECT @FIRSTNAME= Item from dbo.SplitString(@Name, ' ')where ItemIndex=0
 
Share this answer
 
v2
Create below split function

CREATE FUNCTION [dbo].[split](
          @delimited NVARCHAR(MAX),
          @delimiter NVARCHAR(100)
        ) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
        AS
        BEGIN
          DECLARE @xml XML
          SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

          INSERT INTO @t(val)
          SELECT  r.value('.','varchar(MAX)') as item
          FROM  @xml.nodes('/t') as records(r)
          RETURN
        END


and rewrite your script as below
SET @FIRSTNAME= (select val from split(@Name,' ') where ID = 1)

SET @LASTNAME= (select val from split(@Name,' ') where ID = 2)


Thanks,
-RG
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900