SQL server uses stored procedures to query the information of any field

Create the following data table
Insert picture description here

CREATE TABLE EMP(
EMPNO INT  PRIMARY KEY, -- 员工编号
ENAME VARCHAR(10), -- 员工姓名
JOB VARCHAR(9), -- 员工工作
MGR INT, -- 员工直属领导编号
HIREDATE DATE, -- 入职时间
SAL FLOAT, -- 工资
COMM FLOAT, -- 奖金
DEPTNO INT,  -- 所在部门
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO));  -- 关联dept表  

-- ALTER TABLE EMP ADD FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO);
INSERT INTO EMP VALUES(7369,'SMITH','职员',7566,'1980-12-17',800,NULL,20);
INSERT INTO EMP VALUES(7499,'ALLEN','销售员',7698,'1981-02-20',1600,300,30);
INSERT INTO EMP VALUES(7521,'WARD','销售员',7698,'1981-02-22',1250,500,30);
INSERT INTO EMP VALUES(7566,'JONES','经理',7839,'1981-04-02',2975,NULL,20);
INSERT INTO EMP VALUES(7654,'MARTIN','销售员',7698,'1981-09-28',1250,1400,30);
INSERT INTO EMP VALUES(7698,'BLAKE','经理',7839,'1981-05-01',2850,NULL,30);
INSERT INTO EMP VALUES(7782,'CLARK','经理',7839,'1981-06-09',2450,NULL,10);
INSERT INTO EMP VALUES(7788,'SCOTT','职员',7566,'1987-07-03',3000,2000,20);
INSERT INTO EMP VALUES(7839,'KING','董事长',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES(7844,'TURNERS','销售员',7698,'1981-09-08',1500,50,30);
INSERT INTO EMP VALUES(7876,'ADAMS','职员',7566,'1987-07-13',1100,NULL,20);
INSERT INTO EMP VALUES(7900,'JAMES','职员',7698,'1981-12-03',1250,NULL,30);
INSERT INTO EMP VALUES(7902,'FORD','销售员',7566,'1981-12-03',3000,NULL,20);
INSERT INTO EMP VALUES(7934,'MILLER','职员',7782,'1981-01-23',1300,NULL,10);

Now I want to query the number and a certain field information (such as the name of the employee) of the manager in department 10 and the employee in department 30.
I wrote this at first

if (exists (select * from sys.objects where name = 'search'))
    drop proc search
GO
CREATE PROC search(
@some char(5))
AS
	BEGIN
		SELECT EMPNO ,@some FROM EMP 
		WHERE DEPTNO = 10 AND JOB = '经理' 
		OR DEPTNO = 20 AND JOB = '职员';
	END
GO
exec search 'ENAME'

The result of the execution is like this.
Insert picture description here
Then I asked the master who took me and he said that he wanted to change

if (exists (select * from sys.objects where name = 'search'))
    drop proc search
GO
CREATE PROC search(
@some char(5))
AS
	BEGIN
		exec ('SELECT EMPNO ,'+@some+ ' FROM EMP '
		+'WHERE DEPTNO = 10 AND JOB = ''经理'' '
		+'OR DEPTNO = 20 AND JOB = ''职员''');
	END
GO
exec search 'ENAME'

The execution result is as follows.
Insert picture description here
When writing a stored procedure to query any field, pay attention to "field" the incoming variable, that is, if it is directly passed in, during the compilation process, the variable will only be used as a character, not as One to query the field name. The change method is to splice the query statement and the incoming variables in the form of a string, and then execute the spliced ​​query statement.
There is also a big guy who did an analysis comparing various methods. If you are interested, you can take a look at
https://www.cnblogs.com/wy123/p/5958047.html

Guess you like

Origin blog.csdn.net/zhuyin6553/article/details/90297028