DECLARE @dbName NVARCHAR(128);
DECLARE @sql NVARCHAR(MAX);

SELECT TOP 1 @dbName = name
FROM sys.databases
WHERE name LIKE '%NFe%';

IF @dbName IS NOT NULL
BEGIN
    SET @sql = N'
    IF NOT EXISTS (
    SELECT 1 
    FROM ' + QUOTENAME(@dbName) + N'.sys.objects 
    WHERE name = ''EST_NFE_SOLICAPROPCREDCOMBIT'' 
      AND type = N''U''
	)
	BEGIN
		CREATE TABLE ' + QUOTENAME(@dbName) + N'.dbo.EST_NFE_SOLICAPROPCREDCOMBIT (
			ID BIGINT not null,
			SEQ_ITEM     INT not null,
			 VLR_IBS  	money not null ,
			 VLR_CBS  	money not null ,
			 QTD_COMB    	int not null ,
			 UNID_COMB     VARCHAR(6) COLLATE SQL_Latin1_General_CP1_CI_AS	 not null ,
			CONSTRAINT PK_EST_NFE_SOLICAPROPCREDCOMBIT PRIMARY KEY (ID, SEQ_ITEM)
		);
	END';

    EXEC sp_executesql @sql;
END
ELSE
BEGIN
    PRINT 'Nenhum banco de dados de NFe foi encontrado.';
END;

