Not able to allow null values in my sql script

RAP

I'm very beginner in writing sql scripts,

while trying to write the following script and execute it, i could not send null values into the procedure.

could someone help me out from the following code??

ALTER PROCEDURE SP_TALKTRACK2 
    -- Add the parameters for the stored procedure here

    @city VARCHAR(100) = NULL,
    @state VARCHAR(100)= NULL,
    @zip VARCHAR(100) = NULL,
    @gender  VARCHAR(100)= NULL



AS
BEGIN
    /*
    DECLARE @CITY VARCHAR(100)
    DECLARE @STATE VARCHAR(100)
    DECLARE @ZIP VARCHAR(100)
    DECLARE @GENDER  VARCHAR(100) */
    DECLARE @TOTALMAKES INT
    DECLARE @DESCRIPTION VARCHAR(100)
    DECLARE @MAKE2 INT 

    SET @CITY = @city
    SET @STATE = @state
    SET @ZIP = @zip
    SET @GENDER = @gender 


    SET @DESCRIPTION =  ' MAKES ARE BEING USED IN ' + @CITY + ' ' + @STATE + ' ' + @ZIP


    SET @MAKE2 = (SELECT COUNT(MAKE)FROM [TALK TRACK RAP].[DBO].[CARSEXCEL] 
    WHERE CITY = @CITY AND STATE = @STATE AND ZIP = @ZIP AND GENDER = @GENDER)



    SELECT  MAKE , ( (100 * COUNT(*)) /@MAKE2 ) AS MAKECOUNT , 
    CONVERT(VARCHAR, ( (100 * COUNT(*)) /@MAKE2 ))+ ' % ' + MAKE + @DESCRIPTION AS MAKES_PERCENTAGE INTO ##TEMP1
      FROM [TALK TRACK RAP].[DBO].[CARSEXCEL] 
      WHERE CITY = @CITY AND STATE = @STATE AND ZIP = @ZIP AND GENDER = @GENDER
      GROUP BY MAKE 


    SELECT * FROM ##TEMP1 where MAKECOUNT <> 0 order by MAKECOUNT desc

    DROP TABLE ##TEMP1

    SET NOCOUNT ON;

    -- Insert statements for procedure here

END
GO

In the above if i execute leaving one parameter i'm getting null records because i don't have null data

EXEC SP_TALKTRACK2 @CITY = 'ODESSA', @STATE = 'TX', @ZIP = '79762', @GENDER = 'FEMALE' 

But what i need is, it should ignore that parameter and run considering only the other 3 values or 2 values what ever i provide.

Thanks in advance for your help!!

Thanks, Pradeep

Emran Sadeghi

use ISNULL if first parameter is null then return secound parameter for for example ISNULL(@CITY, '') if @CITY is null return empty string becuase secound parameter is empty string this check is required becuse if you add null to string the result is null!

and also in where condation you must check if @city is null dont check city column by null like :

@City is null or City = @City

if your City column also is nullable you can check this :

((@City is null AND City is null) OR (City = @City))

and you'r procedure must like this :

ALTER PROCEDURE SP_TALKTRACK2 
    -- Add the parameters for the stored procedure here

    @city VARCHAR(100) = NULL,
    @state VARCHAR(100)= NULL,
    @zip VARCHAR(100) = NULL,
    @gender  VARCHAR(100)= NULL



AS
BEGIN
    /*
    DECLARE @CITY VARCHAR(100)
    DECLARE @STATE VARCHAR(100)
    DECLARE @ZIP VARCHAR(100)
    DECLARE @GENDER  VARCHAR(100) */
    DECLARE @TOTALMAKES INT
    DECLARE @DESCRIPTION VARCHAR(100)
    DECLARE @MAKE2 INT 

    SET @CITY = @city
    SET @STATE = @state
    SET @ZIP = @zip
    SET @GENDER = @gender 


    SET @DESCRIPTION =  ' MAKES ARE BEING USED IN ' + ISNULL(@CITY, '') + ' ' + ISNULL(@STATE, '') + ' ' + ISNULL(@ZIP, '')


    SET @MAKE2 = (SELECT COUNT(MAKE)FROM [TALK TRACK RAP].[DBO].[CARSEXCEL] 
    WHERE (@city is null OR CITY = @CITY) AND (@state is null or [STATE] = @STATE) AND (@zip is null or ZIP = @ZIP) AND (@gender is null or GENDER = @GENDER))



    SELECT  MAKE , ( (100 * COUNT(*)) /@MAKE2 ) AS MAKECOUNT , 
    CONVERT(VARCHAR, ( (100 * COUNT(*)) /@MAKE2 ))+ ' % ' + MAKE + @DESCRIPTION AS MAKES_PERCENTAGE INTO ##TEMP1
      FROM [TALK TRACK RAP].[DBO].[CARSEXCEL] 
      WHERE  (@city is null OR CITY = @CITY) AND (@state is null or [STATE] = @STATE) AND (@zip is null or ZIP = @ZIP) AND (@gender is null or GENDER = @GENDER)
      GROUP BY MAKE 


    SELECT * FROM ##TEMP1 where MAKECOUNT <> 0 order by MAKECOUNT desc

    DROP TABLE ##TEMP1

    SET NOCOUNT ON;

    -- Insert statements for procedure here

END
GO

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

SQL Select statement - multiple tables allow null values

분류에서Dev

MS Access - sql expression for allow null?

분류에서Dev

My SQL 쿼리의 Null 값

분류에서Dev

Replacing null values in dynamic pivot sql query

분류에서Dev

Why does this SQL order null values last?

분류에서Dev

Why is my duplicate removal script not capturing unique values

분류에서Dev

Inserting Multiple values into a single null columns using sql

분류에서Dev

Return null rows for non-matched values in IN clause in SQL

분류에서Dev

Not able to see radio button list in my screen

분류에서Dev

Not able to install my application on other Android devices

분류에서Dev

My python script when executed prints values to a single column of the excel document in which it is opened

분류에서Dev

Trying to get my Resolve to inject into my controller to be able to inject into the HTML

분류에서Dev

Allow shell script to modify only containing directory

분류에서Dev

Unique constraint on multiple columns - allow for single null

분류에서Dev

Command aliases in my script

분류에서Dev

In my SQL query, how can I have a column with values queried from another table?

분류에서Dev

How do I rewrite my SQL query to remove redundant values from a GROUP BY column?

분류에서Dev

Eliminate NULL values

분류에서Dev

Can't able to run react-native-maps in my Project

분류에서Dev

Why am I not being able to connect to my database?

분류에서Dev

Why is Vim able to defeat my file permissions? OS X

분류에서Dev

Bash script to create script with values embedded

분류에서Dev

My script works in jsFiddle but not in my webpage

분류에서Dev

My script works in jsFiddle but not in my webpage

분류에서Dev

Is it risky to allow every IP on SQL Server?

분류에서Dev

JSON returns [null,null] in my app

분류에서Dev

Why this script is returning ' 'null' is not an object'?

분류에서Dev

How to get the PID of my script?

분류에서Dev

Having problems with my bash script

Related 관련 기사

  1. 1

    SQL Select statement - multiple tables allow null values

  2. 2

    MS Access - sql expression for allow null?

  3. 3

    My SQL 쿼리의 Null 값

  4. 4

    Replacing null values in dynamic pivot sql query

  5. 5

    Why does this SQL order null values last?

  6. 6

    Why is my duplicate removal script not capturing unique values

  7. 7

    Inserting Multiple values into a single null columns using sql

  8. 8

    Return null rows for non-matched values in IN clause in SQL

  9. 9

    Not able to see radio button list in my screen

  10. 10

    Not able to install my application on other Android devices

  11. 11

    My python script when executed prints values to a single column of the excel document in which it is opened

  12. 12

    Trying to get my Resolve to inject into my controller to be able to inject into the HTML

  13. 13

    Allow shell script to modify only containing directory

  14. 14

    Unique constraint on multiple columns - allow for single null

  15. 15

    Command aliases in my script

  16. 16

    In my SQL query, how can I have a column with values queried from another table?

  17. 17

    How do I rewrite my SQL query to remove redundant values from a GROUP BY column?

  18. 18

    Eliminate NULL values

  19. 19

    Can't able to run react-native-maps in my Project

  20. 20

    Why am I not being able to connect to my database?

  21. 21

    Why is Vim able to defeat my file permissions? OS X

  22. 22

    Bash script to create script with values embedded

  23. 23

    My script works in jsFiddle but not in my webpage

  24. 24

    My script works in jsFiddle but not in my webpage

  25. 25

    Is it risky to allow every IP on SQL Server?

  26. 26

    JSON returns [null,null] in my app

  27. 27

    Why this script is returning ' 'null' is not an object'?

  28. 28

    How to get the PID of my script?

  29. 29

    Having problems with my bash script

뜨겁다태그

보관