Two stored procedures: 1. Use the stored procedure to dynamically create a data table. 2. Insert a new record.

Source: Internet
Author: User

Recently, SQL stored procedures have been found to greatly reduce the amount of code in the program.

In the future, we need to consider using more stored procedures.

Two tips:

1. Use stored procedures to dynamically create data tables.

/*
Process function: dynamically create a data table in a stored procedure
Note: connect the "single quotation marks" of the SQL statement"
Created on: 2006-09-01
Created by: skyjacker
Blog: http: // blog.csdn.net/skyjacker
*/
Create procedure [DBO]. [createtable]
@ Tablename varchar (255)
As
Begin
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. ['+ @ tablename +'] ') and

Objectproperty (ID, N 'isusertable') = 1)
Exec ('drop table' + @ tablename );

Exec ('
Create Table [DBO]. ['+ @ tablename +'] (
[ID] [varchar] (50) Collate chinese_prc_ci_as null,
[Name] [varchar] (50) Collate chinese_prc_ci_as null
) On [primary] ')
End;
Go

Execute exec createtable 'tabhello'

2. Insert a new record.
/*
Process functions:
Insert new record
If yes, no insert
Insert if it does not exist
Note: There should be better methods
Created on: 2006-09-01
Created by: skyjacker
Blog: http: // blog.csdn.net/skyjacker
*/
Create procedure lyrtypetable
@ Maplayer varchar (255 ),
@ Type varchar (255 ),
@ Drawtool varchar (255)
As
Begin
Declare @ recordnum integer
Set @ recordnum = 0
Select top 1 maplayer from tabtest where maplayer = @ maplayer

Set @ recordnum = @ rowcount
If (@ recordnum = 0)
Insert into tabtest (maplayer, type, drawtool) values (@ maplayer, @ type, @ drawtool)
End;
Go

 

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.