Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Stored Procedure

$
0
0

DROP PROCEDURE CODES

GO

CREATE PROCEDURE CODES(@P_FCODE BIGINT,@P_LCODE BIGINT,@P_TNAME VARCHAR(50))
AS
BEGIN 
DECLARE C1 CURSOR FOR SELECT caption,codestart,codeend,codecat
from hcustom where caption like '%Classes%'

declare @v_caption varchar(50),@v_codestart bigint,@V_codeend bigint,@v_codecat CHAR(1)
,@V_stmt varchar(4000)

open c1
fetch next from c1 into @v_caption,@v_codestart,@v_codeend,@v_codecat
while (@@fetch_Status=0)
BEGIN
print @v_caption

-- prepare sql stement and execute it

set @v_stmt ='CREATE TABLE '+@P_TNAME+'( CODENUMBER  BIGINT,CODEDESC VARCHAR(50),CODEAMT NUMERIC(12,5))'

print @v_Stmt
EXEC (@v_Stmt)
fetch next from c1 into @v_caption,@v_codestart,@v_codeend,@v_codecat


END
CLOSE C1
deallocate c1

declare c2 cursor for select codenumber,codedesc,codeamt from hccode,hcustom where codenumber between codestart and codeend
declare @v_codenumber bigint,@v_codedesc varchar(50),@v_codeamt bigint,@V_smt varchar(4000)

open c2
fetch next from c2 into @v_codenumber,@v_codedesc,@v_codeamt
while (@@fetch_Status=0)
BEGIN
print @v_codenumber

set @v_stmt= 'INSERT INTO' + @P_TNAME +'(CODENUMBER, CODEDESC, CODEAMT) VALUES' + '(@v_codenumber,@v_codedesc,@v_codeamt)';

    print @v_stmt
   --EXEC(@v_smt)
    fetch next from c2 into @v_codenumber,@v_codedesc,@v_codeamt

    END
    CLOSE c2
    deallocate c2
 

  END
GO
exec codes 451,475,'classes_test'

I am able to execute this without any errors but the data is not inserted into the new table from the old one. Please help me regarding this. I have two tables hcustom and hccode from where I need to fill the data into classes_test table which is a part of hcustom

       

Viewing all articles
Browse latest Browse all 8428

Trending Articles