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.