Use SQL Server Add delete modify query stored procedure

Source: Internet
Author: User

--Add

CREATE PROCEDURE Usp_add

(

@table nvarchar (255),

@values nvarchar (max) =null

)

As

declare @sql nvarchar (max)

Set @sql = ' INSERT INTO ' + @table

If @values is not null

Set @sql = ' INSERT INTO ' + @table + ' values (' + @values + ') '

EXEC sp_executesql @sql

SELECT @ @IDENTITY

Go

EXEC usp_add ' Jinshan shares ', ' abc ', 20,300 '

Go

--Delete

CREATE PROCEDURE Usp_delete

(

@table nvarchar (255),

@where nvarchar (max) =null

)

As

declare @sql nvarchar (max)

Set @sql = ' Delete ' + @table

If @where is not null

Set @sql + = ' where ' + @where

EXEC sp_executesql @sql

Go

EXEC usp_delete ' Jinshan shares ', ' id=1 '

Go

--Modify

CREATE PROCEDURE Usp_update

(

@table nvarchar (255),

@set nvarchar (max),

@where nvarchar (max) =null

)

As

declare @sql nvarchar (max)

Set @sql = ' Update ' + @table + ' Set ' + @set

If @where is not null

Set @sql + = ' where ' + @where

EXEC sp_executesql @sql

Go

EXEC usp_update ' Jinshan shares ', ' stockname= ' ' Tencent shares ', ' id=2 '

Go

--Find

CREATE PROCEDURE Usp_select

(

@table nvarchar (255),

@where nvarchar (max) =null

)

As

declare @sql nvarchar (max)

Set @sql = ' select * from ' + @table

If @where is not null

Set @sql = @sql + ' where ' + @where

EXEC sp_executesql @sql

Go

EXEC usp_select ' stock ', ' id=1 '

Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.