none
What is meant by %d in the sql server code... RRS feed

  • Question

  • Hi Experts,

    In the piece of already written code by some one in my project, i saw the %d  as shown in the below context.

    Declare @count INT
      SELECT @count = COUNT(1) FROM dbo.test 
      RAISERROR('COUNT %d', 0, 1, @count) WITH NOWAIT

    here dbo.test is the user defined table that holds some records.

    have any body has idea about %d in the above code.

    Thanks in Advance.

    Friday, February 22, 2019 2:16 PM

Answers

  • Hi

    Select d,CONVERT(DATE,d),CONVERT(CHAR(10),D,120), FORMAT(d,'yyyy-MM-dd'

    FROM dbo.dtest;

    --------------------------------------

    declare @score int

    set @scor=80

    declare @name varchar(50)

    set @name='Jone'

    declare @reuslt varchar(500)

    exec master..xp_printf @result, 'Name %s, Score %d',@name,@score

    PRINT @result

    ---------------

    Best Regards.



    Monday, February 25, 2019 8:34 AM
  • Hi,
    In raiserror, the message string cannot be concatenated with +, so to raise an error message with 'COUNT' concatenated with @Count variable, you have to use tags like %d. 

    The %d is replaced by the value of @Count.


    Ousama EL HOR

    [If a post helps to resolve your issue, please click the "Mark as Answer"  of that post or click Answered "Vote as helpful"  button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


     [User Page]     [MSDN Page]     [Blog]     [Linkedin]

    Friday, February 22, 2019 2:43 PM
  • See RAISERROR (Transact-SQL) => msg_str + type for how it works.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Friday, February 22, 2019 3:21 PM
    Moderator

All replies

  • Hi,
    In raiserror, the message string cannot be concatenated with +, so to raise an error message with 'COUNT' concatenated with @Count variable, you have to use tags like %d. 

    The %d is replaced by the value of @Count.


    Ousama EL HOR

    [If a post helps to resolve your issue, please click the "Mark as Answer"  of that post or click Answered "Vote as helpful"  button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


     [User Page]     [MSDN Page]     [Blog]     [Linkedin]

    Friday, February 22, 2019 2:43 PM
  • See RAISERROR (Transact-SQL) => msg_str + type for how it works.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Friday, February 22, 2019 3:21 PM
    Moderator
  • Hi Ramesh,

     

    After SQL Server 2012,Microsoft suggests us to start using THROW statement instead of RAISERROR. THROW statement is simple and easy to use than RAISERROR.

     

    For more details, please refer to https://docs.microsoft.com/en-us/sql/t-sql/language-elements/throw-transact-sql?view=sql-server-2017

     

    Best regards,

    Dedmon Dai


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, February 25, 2019 8:16 AM
  • Hi

    Select d,CONVERT(DATE,d),CONVERT(CHAR(10),D,120), FORMAT(d,'yyyy-MM-dd'

    FROM dbo.dtest;

    --------------------------------------

    declare @score int

    set @scor=80

    declare @name varchar(50)

    set @name='Jone'

    declare @reuslt varchar(500)

    exec master..xp_printf @result, 'Name %s, Score %d',@name,@score

    PRINT @result

    ---------------

    Best Regards.



    Monday, February 25, 2019 8:34 AM