SQL Query with where clause

RIAS
RIAS used Ask the Experts™
on
Hello,

Need to have a new column depending on the text in an column.
Please find the attached example.

Thanks in advance.
example4.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
try like:

Select ApprovalDocID, ApprovalDocDisplay, Email ,
case when isnull(Email , '') = '' then 'Guest' else 'Employee' end Requestor
from your Table

Open in new window

Author

Commented:
Thanks,
Can you fit the above in the stored procedure format.

Author

Commented:
ALTER PROCEDURE  [dbo].[PROC1]

(
    
	 @DocIdVal varchar(100) = null
	
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  	
	
	declare @SQL varchar(500) = null
	declare @GroupBy varchar(500) = null
	-- NONCLUSTERED INDEX [NIX__UNQ__UID_]

	SET @SQL = 'SELECT  * ' 

	IF @DocIdVal <> ''  
		BEGIN
			SET @SQL = @SQL + '   FROM  [View_XYZ]    ' 
		    SET @SQL = @SQL + ' WHERE  ApprovalDocID = '+ '''' + CAST(@DocIdVal  AS VARCHAR(255))  +''''	
		END
		  
	SET @SQL = @SQL  + ISNULL(@GroupBy,'')
     		
	  
	EXEC(@SQL)
END

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ryan ChongSoftware Team Lead

Commented:
Can you fit the above in the stored procedure format.
it depends on what you want to do in a stored procedure, basically stored procedure is running a piece of codes.

without further info, we may not able to provide further suggestion

Author

Commented:
please find my Sp in the above comment
Software Team Lead
Commented:
try this:

ALTER PROCEDURE  [dbo].[PROC1]

(
    
	 @DocIdVal varchar(100) = null
	
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;  	
	
	declare @SQL varchar(500) = null
	declare @GroupBy varchar(500) = null
	-- NONCLUSTERED INDEX [NIX__UNQ__UID_]

	SET @SQL = 'SELECT  * , case when isnull(Email , '''') = '''' then ''Guest'' else ''Employee'' end Requestor ' 

	IF @DocIdVal <> ''  
		BEGIN
			SET @SQL = @SQL + '   FROM  [View_XYZ]    ' 
		    SET @SQL = @SQL + ' WHERE  ApprovalDocID = '+ '''' + CAST(@DocIdVal  AS VARCHAR(255))  +''''	
		END
		  
	SET @SQL = @SQL  + ISNULL(@GroupBy,'')
     		
	  
	EXEC(@SQL)
END

Open in new window

ste5anSenior Developer

Commented:
Can you post more context, why you are doing this? Cause that SP seems to be using dynamic SQL without absolutely any reason.

Author

Commented:
Perfect!!! Thanks a ton!

Author

Commented:
Ste5an,
Thanks really appreciate your help. But, the query is resolved.

Thanks once again.
ste5anSenior Developer

Commented:
I don't doubt that you have a solution, but the context and solution have code smells. This is not a good situation.

For example: call your procedure with @DocIdVal = '', which is an allowed input value. There is no parameter validation. And if the DocId is really an arbitrary string, then it may contain quotation marks. This leads to SQL injection. Sad, very sad.
Also you have an unnecessary cast. And you're using sub-optimal data types. Dead code is also found.

So a solution should at least look like

ALTER PROCEDURE [dbo].[PROC1] (
    @DocIdVal VARCHAR(100) = NULL
)
AS
    SET NOCOUNT ON;

    DECLARE @Statement NVARCHAR(MAX) = N'
        SELECT * ,
               CASE WHEN ISNULL(Email, '''') = '''' THEN ''Guest''
                    ELSE ''Employee''
               END AS Requestor
        FROM   [View_XYZ]
        WHERE  ApprovalDocID = @DocIdVal;
    ';

    IF ( @DocIdVal <> '' )
        BEGIN
            SET @Statement = REPLACE(@Statement, '@DocIdVal', '''' + REPLACE(@DocIdVal, '''', '''''') + '''');
            EXECUTE ( @Statement );
        END;
    ELSE
        PRINT 'No data.';

Open in new window


Then this raises questions about your data model: Why can the email column contain an empty string? There should be a constraint only allowing at least strings of the simple '%@%.%' pattern.

And last, but not least: Why not using a view or a computed column instead?

Author

Commented:
Perfect mate!!! Thanks for these important suggestions .

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial