CREATE PROCEDURE USP_CREATEINSERTPROC (@TBLNAME VARCHAR(100))
AS
BEGIN
DECLARE @QRY NVARCHAR(MAX)=''
SET @QRY='CREATE PROCEDURE USP_INSERT_'+@TBLNAME+'('
SELECT @QRY=@QRY+'@'+COLUMN_NAME+' '+DATA_TYPE+CASE WHEN DATA_TYPE='VARCHAR' THEN '('+CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(100))+')' ELSE ''END
+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TBLNAME
SET @QRY=STUFF(@QRY,LEN(@QRY),1,'')+')'
SET @QRY=@QRY+CHAR(13)+'AS'+CHAR(13)+'BEGIN'
SET @QRY=@QRY+CHAR(13)+'INSERT INTO '+@TBLNAME +' VALUES ('
SELECT @QRY=@QRY+'@'+COLUMN_NAME+',' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@TBLNAME
SET @QRY=STUFF(@QRY,LEN(@QRY),1,'')+')'
SET @QRY=@QRY+CHAR(13)+'END'
EXEC( @QRY)
END
EXEC USP_CREATEINSERTPROC 'DEPT'
-------SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='DEPT'
DROP PROCEDURE USP_INSERT_EMP
CREATE PROCEDURE USP_INSERT_EMP(@EMPNO NUMERIC,@ENAME VARCHAR(10),@JOB VARCHAR(9),@MGR NUMERIC,@HIREDATE DATETIME,@SAL NUMERIC,@COMM NUMERIC,@DEPTNO NUMERIC)
AS
BEGIN
INSERT INTO EMP VALUES (@EMPNO,@ENAME,@JOB,@MGR,@HIREDATE,@SAL,@COMM,@DEPTNO)
END
SELECT * FROM EMP
EXEC USP_INSERT_EMP 20,'RAM','TST',25,'1980-12-17 00:00:00.000',5000,NULL,50
No comments:
Post a Comment