Several stored procedures created by SQL

Source: Internet
Author: User

Create a stored procedure
You can store table names and comparison fields as parameters.
Create PROCEDURE sp_getTeacherInfo
@ TblName nvarchar (30), -- table name
@ CmpStr nvarchar (30), -- value used for comparison
@ Tblasd nvarchar (30) -- fields involved in comparison
AS
DECLARE @ SQL varchar (4000)
SET @ SQL = 'select * from' + @ TblName + 'where' + @ tblrecognition + '=' + @ CmpStr
EXEC (@ SQL)


Table tbl_TeacherInfo


Exec sp_getTeacherInfo 'tbl _ teacherinfo', 'teano ', '123'
// Note: a call like this is incorrect.
Restore to query statement
Select * from tbl_TeacherInfo where TeaNo = 07417502
The error is reported because the '123' parameter is mistaken for an integer and compared with an integer.

Exec sp_getTeacherInfo 'tbl _ teacherinfo', 'name', 'chu Liuxiang'
// Error
Restore to query statement
Select * from tbl_TeacherInfo where TeaNo = Chu Liuxiang
// It is obviously incorrect.

Correct call Method
Exec sp_getTeacherInfo 'tbl _ teacherinfo', 'name', "'chu Liuxiang '"
Restore to query statement
Select * from tbl_TeacherInfo where TeaNo = 'chu Liuxiang'

Create a general Stored Procedure
Create PROCEDURE sp_AddRowToLogin
@ TeaNo nvarchar (100), -- Compare Fields
@ TeaName nvarchar (100) -- Comparison Field
AS
Insert into tbl_UserLogin values (@ TeaNo, @ TeaName, @ TeaNo, 0)


Stored Procedure returned by all columns except the specified Column
Create procedure sp_Alter
@ TblName nvarchar (30) -- table name
AS
Declare @ SQL varchar (1000)
Select @ SQL = 'select'
Select @ SQL = @ SQL + name + ',' from syscolumns where id = object_id (@ TblName) and name not in ('id', 'teano ')
Select @ SQL = left (@ SQL, len (@ SQL)-1)
Select @ SQL = @ SQL + 'from' + @ TblName
Exec (@ SQL)

// No results are returned except for the ID and TeaNo columns.

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.