从存储过程进行正常查询

发布于 2024-12-17 09:10:46 字数 1633 浏览 2 评论 0原文

使用 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 the emp_code or If the values is "001" then query return the "001" emp_code only

  • In the 2nd Parameter (Company_Code): if the value is "All" means then query return the all the emp_code for all the company (ex: IBM, SoftTech, etc) or If the value is "IBM" means then query return all the emp_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-12-24 09:10:46

下面是更简单的查询,请尝试这样做:

SELECT PERSONID FROM T_PERSON WHERE 
    CCODE = (
        CASE WHEN @CCODE = 'ALL'  THEN CCODE
        ELSE @CCODE END
        )
    AND
    EMPCODE = (
        CASE WHEN @EMPCODE = 'ALL'  THEN EMPCODE
        ELSE @EMPCODE
        END
        )

Below is the simpler query, please try with this:

SELECT PERSONID FROM T_PERSON WHERE 
    CCODE = (
        CASE WHEN @CCODE = 'ALL'  THEN CCODE
        ELSE @CCODE END
        )
    AND
    EMPCODE = (
        CASE WHEN @EMPCODE = 'ALL'  THEN EMPCODE
        ELSE @EMPCODE
        END
        )
垂暮老矣 2024-12-24 09:10:46

您不能直接在存储过程之外的 SQL 中执行此操作,您的存储过程有多个 if/else 语句,sql 查询中不存在执行此操作的结构,如果您想实现此目的,您将需要构建脚本逻辑使用(即 c#/php/coldfusion/etc)访问它的语言,然后执行单独的查询。

您使用什么语言调用存储过程,还是直接从 SQL 服务器调用?

编辑:我有一段时间没有使用 VB6,所以我不记得如何在 VB 中编写此内容,但您基本上会从存储过程复制逻辑来构建查询,然后发送查询直接来自 VB,不让存储过程执行此操作。

例如

 Dim sql As String
 If CCODE <> 'All'
      sql = 'SELECT PERSONID FROM T_PERSON WHERE AND (CCODE IN (' & CCODE & '));'
      {send the query to sql server}
 End If

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.

 Dim sql As String
 If CCODE <> 'All'
      sql = 'SELECT PERSONID FROM T_PERSON WHERE AND (CCODE IN (' & CCODE & '));'
      {send the query to sql server}
 End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文