从存储过程进行正常查询
使用 VB6 和 SQL Server 2000
我想将存储过程转换为普通查询
存储过程:
Alter PROC [dbo].[proc_New]
@CCODE VARCHAR(100),
@EmpCode VARCHAR(100)
AS
BEGIN
DECLARE @ID VARCHAR (5)
DECLARE @Des VARCHAR(10)
DECLARE @SQL VARCHAR(1000)
DECLARE @Flag INT
SELECT @Flag=0
SELECT @SQL = 'SELECT PERSONID FROM T_PERSON WHERE '
IF @CCODE<>'All'
BEGIN
IF @Flag=1
BEGIN
SELECT @SQL = @SQL+' AND (CCODE IN ('''+@CCODE+'''))'
END
ELSE
BEGIN
SELECT @SQL = @SQL+' (CCODE IN ('''+@CCODE+'''))'
SELECT @Flag=1
END
END
IF @EMPCODE<>'All'
BEGIN
IF @Flag=1
BEGIN
SELECT @SQL = @SQL+' AND (EMPCODE IN ('''+@EMPCODE+'''))'
END
ELSE
BEGIN
SELECT @SQL = @SQL+' (EMPCODE IN ('''+@EMPCODE+'''))'
SELECT @Flag=1
END
END
IF @SQL = 'SELECT EmpCode FROM EMPMST WHERE ' SELECT @SQL = 'SELECT EmpCode FROM EMPMST'
INSERT INTO table EXEC(@SQL)
GO
过程说明...
我传递 2 个参数值,例如 emp_code 或 All 和 company_code 或 All。
第一个参数(
Emp_Code
):如果值为“All”则查询返回所有emp_code
,如果值为“001”则查询仅返回“001”emp_code
在第二个参数 (
Company_Code
) 中:如果值为“All”表示则查询返回所有公司(例如:IBM、SoftTech 等)的所有emp_code
或如果值为“IBM”表示则查询返回所有emp_code
代表该公司 (IBM)
以上存储过程是工作正常,但我想要转换为普通查询。
任何人都可以帮助我
需要查询帮助
Using VB6 and SQL Server 2000
I want to convert a stored procedure to normal query
Stored procedure:
Alter PROC [dbo].[proc_New]
@CCODE VARCHAR(100),
@EmpCode VARCHAR(100)
AS
BEGIN
DECLARE @ID VARCHAR (5)
DECLARE @Des VARCHAR(10)
DECLARE @SQL VARCHAR(1000)
DECLARE @Flag INT
SELECT @Flag=0
SELECT @SQL = 'SELECT PERSONID FROM T_PERSON WHERE '
IF @CCODE<>'All'
BEGIN
IF @Flag=1
BEGIN
SELECT @SQL = @SQL+' AND (CCODE IN ('''+@CCODE+'''))'
END
ELSE
BEGIN
SELECT @SQL = @SQL+' (CCODE IN ('''+@CCODE+'''))'
SELECT @Flag=1
END
END
IF @EMPCODE<>'All'
BEGIN
IF @Flag=1
BEGIN
SELECT @SQL = @SQL+' AND (EMPCODE IN ('''+@EMPCODE+'''))'
END
ELSE
BEGIN
SELECT @SQL = @SQL+' (EMPCODE IN ('''+@EMPCODE+'''))'
SELECT @Flag=1
END
END
IF @SQL = 'SELECT EmpCode FROM EMPMST WHERE ' SELECT @SQL = 'SELECT EmpCode FROM EMPMST'
INSERT INTO table EXEC(@SQL)
GO
Procedure Explanation...
I am passing 2 parameter values like emp_code or All and company_code or All.
In the 1st parameter (
Emp_Code
): if the value is "All" means then query return the all theemp_code
or If the values is "001" then query return the "001"emp_code
onlyIn the 2nd Parameter (
Company_Code
): if the value is "All" means then query return the all theemp_code
for all the company (ex: IBM, SoftTech, etc) or If the value is "IBM" means then query return all theemp_code
for that company (IBM)
Above stored procedure is working fine, but I want to convert into normal query.
Can anybody help me
Need query help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是更简单的查询,请尝试这样做:
Below is the simpler query, please try with this:
您不能直接在存储过程之外的 SQL 中执行此操作,您的存储过程有多个 if/else 语句,sql 查询中不存在执行此操作的结构,如果您想实现此目的,您将需要构建脚本逻辑使用(即 c#/php/coldfusion/etc)访问它的语言,然后执行单独的查询。
您使用什么语言调用存储过程,还是直接从 SQL 服务器调用?
编辑:我有一段时间没有使用 VB6,所以我不记得如何在 VB 中编写此内容,但您基本上会从存储过程复制逻辑来构建查询,然后发送查询直接来自 VB,不让存储过程执行此操作。
例如
You can't directly do this in SQL outside of a stored procedure, your stored procedure has several if/else statements, no structure exists in sql queries to do this, if you want to achieve this you will need to build in sripting logic from the language accessing it with (i.e. c#/php/coldfusion/etc) and then perform the individual queries.
What Language are you calling the stored procedure from, or is this directly from the SQL server?
Edit: I have not used VB6 in a while so I don't remember how to write this in VB but you would basically be copying the logic from the stored procedure to build the query and then sending the query directly from VB not letting the stored procedure do it.
e.g.