Check if table contains data per company
info
Before running the command you need to change
@DatabaseName
@TableName
@AppID
@PrintCommand
@SelectAll
IF OBJECT_ID (N'[dbo].ReplaceCharsInCompanyName') IS NOT NULL
DROP FUNCTION [dbo].ReplaceCharsInCompanyName;
GO
CREATE FUNCTION [dbo].ReplaceCharsInCompanyName(@CharsToReplace VARCHAR(MAX), @ReplaceWith CHAR(1), @ReplaceIn VARCHAR(30))
RETURNS VARCHAR(30)
AS
BEGIN
DECLARE @i INT
SET @i = 0
WHILE @i < LEN(@CharsToReplace)
BEGIN
SELECT @i = @i + 1
SET @ReplaceIn = REPLACE(@ReplaceIn, SUBSTRING(@CharsToReplace, @i, 1), @ReplaceWith)
END
RETURN @ReplaceIn
END
GO
DECLARE @DatabaseName VARCHAR(128), @TableName VARCHAR(128), @AppID VARCHAR(50), @PrintCommand BIT, @SelectAll BIT
-------------------------------PRE STEP-------------------------------
SET @DatabaseName = ''
SET @TableName = ''
SET @AppID = ''
SET @PrintCommand = 0
SET @SelectAll = 0
-------------------------------DO NOT EDIT UNDER THIS LINE-------------------------------
DECLARE @CompaniesQuery VARCHAR(200),
@Query VARCHAR(MAX),
@BlankChar VARCHAR(10)
SET @CompaniesQuery = 'SELECT [Name] FROM [' + @DatabaseName + '].[dbo].[Company]'
DECLARE @CompaniesFormatted TABLE(CompanyName VARCHAR(30))
INSERT INTO @CompaniesFormatted EXEC (@CompaniesQuery)
DECLARE CompaniesCurser CURSOR FOR SELECT CompanyName from @CompaniesFormatted
DECLARE @CompanyName VARCHAR(MAX)
OPEN CompaniesCurser
FETCH CompaniesCurser INTO @CompanyName
WHILE @@FETCH_STATUS != -1
BEGIN
SELECT @CompanyName = [dbo].ReplaceCharsInCompanyName('."\/''%][', '_', @CompanyName)
PRINT @CompanyName
SET @Query = 'SELECT';
if @SelectAll = 1
SET @Query = @Query + ' *';
ELSE
SET @Query = @Query + ' COUNT(*)';
IF @AppID = ''
SET @Query = @Query + ' FROM [' + @DatabaseName + '].[dbo].[' + @CompanyName + '$' + @TableName + ']';
ELSE
SET @Query = @Query + ' FROM [' + @DatabaseName + '].[dbo].[' + @CompanyName + '$' + @TableName + '$' + @AppID + ']';
IF @PrintCommand = 1
PRINT @Query
ELSE
EXEC (@Query)
FETCH CompaniesCurser INTO @CompanyName
END
CLOSE CompaniesCurser
DEALLOCATE CompaniesCurser
GO
IF OBJECT_ID (N'[dbo].ReplaceCharsInCompanyName') IS NOT NULL
DROP FUNCTION [dbo].ReplaceCharsInCompanyName;