Skip to main content

Posts

Showing posts from 2016

Recursive Scalar Function in T-SQL

CREATE FUNCTION dbo.Fibonacci (@Num integer, @prev integer, @next integer) RETURNS VARCHAR(4000) AS BEGIN DECLARE @returnValue as VARCHAR (4000) = cast(@prev as varchar(4000)); IF (@Num > 0) BEGIN IF (LEN(@returnValue) > 0) BEGIN SET @returnValue = @returnValue + ','; END SET @returnValue = @returnValue + dbo.Fibonacci(@Num - 1, @next, @next + @prev) ; END RETURN @returnValue; END GO ---------------get list select dbo.Fibonacci(10,0,1)

Query for Automatic find table and set seed value in SQL Server

Declare @t table(Id INT identity(1,1), tName nvarchar(max), cName nvarchar(max)) Declare @t1 table(ID INT,tName nvarchar(max), cCount BIGINT) Declare @SQry nVarchar(MAX) = '', @tName nvarchar(max), @cName nvarchar(max) Declare @nCount int,@sCount int=1 Insert into @t(tName,cName) select a.name,b.name from sys.tables a inner join sys.all_columns b on a.object_id = b.object_id where b.is_identity = 1 select @nCount = COUNT(*) from @t While (@sCount <= @nCount) BEGIN select @tName = tname,@cName = cName from @t where id = @sCount --print N'select max('+ @cName +') nCount, '''+ @tName +''' Tname from '+ @tName +' having IDENT_CURRENT( '''+ @tName +''') <> max('+ @cName +')' Insert into @t1(ID,cCount, tName) exec (N'select '+ @sCount +', max('+ @cName +') nCount, '''+ @tName +''' Tname from '+ @tName +' having IDENT_C...