Several stored procedures created by SQL

Source: Internet
Author: User
Tags comparison sql query table name

Creating a Stored Procedure
Table name and comparison field a stored procedure that can be used as a parameter
Create PROCEDURE Sp_getteacherinfo
@TblName nvarchar (30),--table name
@CmpStr nvarchar (30),--value to participate in the comparison
@TblAtr nvarchar (30)--fields that participate in the comparison
As
DECLARE @sql varchar (4000)
SET @sql = "SELECT * from" + @TblName + "where" + @TblAtr + "=" + @CmpStr
EXEC (@sql)


Table Tbl_teacherinfo


Exec sp_getteacherinfo "Tbl_teacherinfo", "Teano", "07417502"
Note: Calls like this are wrong
Revert to query statement
SELECT * from tbl_teacherinfo where Teano = 07417502
The reason why there is no error is that the parameter "07417502" is mistaken for an integral type, and a comparison of integers

Exec sp_getteacherinfo "Tbl_teacherinfo", "Name", "Chu Liuxiang"
Error
Revert to query statement
SELECT * from tbl_teacherinfo where Teano = Chu Liuxiang
It's obviously wrong.

Correct method of calling
Exec sp_getteacherinfo "Tbl_teacherinfo", "Name", "Chu Liuxiang" "
Revert to query statement
SELECT * from tbl_teacherinfo where Teano = "Chu Liuxiang"

Creation of general stored procedures
Create PROCEDURE Sp_addrowtologin
@TeaNo nvarchar (100),--comparing fields
@TeaName nvarchar (100)--comparing fields
As
INSERT into Tbl_userlogin values (@TeaNo, @TeaName, @TeaNo, 0)


Stored procedures that are returned except for 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 isn't in ("id", "Teano")
Select @sql =left (@sql, Len (@sql)-1)
Select @sql = @sql + "from" + @TblName
EXEC (@sql)

Except that the ID and Teano two columns do not return, the other returns



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.