Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
hi,
am working on a project which import data into sqlserver,i have written SP for insertion but at the same time i need to update the data in sql server
i have written sample SP ,that SP is inserting and updating last row in the excelsheet ,i need to import data from excelsheet to sql and need to update the data in sqlserver

sample sp

SQL
CREATE proc [dbo].[ConvertCharToBinary1](@Value varchar(8000))
as
Begin
declare @Flag int;

    IF EXISTS(select *from [dbo].table1)
    begin
    set @Flag = 1;
    end
    else
    begin
    set @Flag = 0;
    end
if @Flag = 0
        begin
insert into [dbo].table1([Char_c]) values(cast(CAST(@Value as char(8000)) as binary(8000)))
End
else
begin
UPDATE table1 SET  [char_c]=(cast(cast(@Value as char(4000))as binary(4000)))
        End
End
GO
Posted

1 solution

A stored procedure is called and the data needs to be updated if it already exists and inserted if it does not.
SQL
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
    UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
    INSERT INTO Table1 VALUES (...)




This solution for above Question

CREATE proc [dbo].[ConvertCharToBinary1](@Value3 varchar(8000),@Value1 varchar(8000))
as

Begin
IF EXISTS (select * from [dbo].table1 WHERE [int_c]=convert(binary,cast(@Value3 as int)))

UPDATE table1 SET [int_c]=convert(binary,cast(@Value3 as int)),
[char_c]=(cast(cast(@Value1 as char(4000))as binary(4000))) where [int_c]=convert(binary,cast(@Value3 as int))


ELSE


insert into [dbo].table1([int_c],[char_c]) values
(
convert(binary,cast(@Value3 as int)),
cast(CAST(@Value1 as char(50)) as binary(50))

)


End
GO
 
Share this answer
 
v4

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