SQL Query with where clause

Hello,

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

Thanks in advance.
example4.xlsx
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try like:

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

Open in new window

0
RIASAuthor Commented:
Thanks,
Can you fit the above in the stored procedure format.
0
RIASAuthor 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

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
0
RIASAuthor Commented:
please find my Sp in the above comment
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Can you post more context, why you are doing this? Cause that SP seems to be using dynamic SQL without absolutely any reason.
0
RIASAuthor Commented:
Perfect!!! Thanks a ton!
0
RIASAuthor Commented:
Ste5an,
Thanks really appreciate your help. But, the query is resolved.

Thanks once again.
0
ste5anSenior DeveloperCommented:
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?
0
RIASAuthor Commented:
Perfect mate!!! Thanks for these important suggestions .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.