SQL调着玩

CREATE FUNCTION [dbo].[IsInteger]
(
  @Value NVARCHAR(4000)
) RETURNS NVARCHAR(4000)
AS
BEGIN
   DECLARE @ResultValue BIT-- = 1
   --如果IS NULL把值设为N''
   SET @Value = ISNULL(LTRIM(@Value), N'')
   --处理前导N'-'字符串,如果有,把它移除,这样才好做以下的判断。
   SET @Value = CASE WHEN SUBSTRING(@Value, 1, 1) = N'-' THEN SUBSTRING(@Value, 2, LEN(@Value) - 1) ELSE @Value END

    --如不是数据或为N''返加Faluse
   IF @Value LIKE '%[^0-9]%' OR @Value = N''
      SET @ResultValue = 0
  
   RETURN @ResultValue
END

 

/* SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        Insus.NET
-- Create date: 2012-02-23
-- Description:    Calculate the number of characters in the string */
-- =============================================

CREATE PROCEDURE [dbo].[CalNumOfChtInStrads]

    @Value NVARCHAR(4000)

AS
BEGIN
    DECLARE @dum TABLE ([Str] NVARCHAR(2))
    DECLARE @I INT ,@J INT

set @I= LEN(@Value)
set @J= LEN(@Value)
    WHILE @I > 0
    BEGIN
        --以下函数可参考:http://www.cnblogs.com/insus/archive/2011/06/25/2090231.html
        IF [dbo].[IsInteger] (SUBSTRING(@Value, @I, 1)) = 0
        BEGIN           
            RAISERROR('传入字符串包含其它字符,不完全是数字。',16,1)           
            RETURN
        END   
        SET @I = @I - 1
    END
   
    WHILE @J > 0
    BEGIN
        INSERT INTO @dum VALUES(SUBSTRING(@Value, @J, 1))
        SET @J = @J - 1
    END
   
   SELECT [Str],COUNT([Str]) AS [Num] FROM @dum GROUP BY [Str]
END


EXECUTE [dbo].[CalNumOfChtInStrads] '5487554127489423454'

你可能感兴趣的:(别人编写的----调过)