Use [DBName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Declare @MainTableNames TABLE(TableName varchar(100))
Declare @TransmissionTableName TABLE (TransTableName varchar(100), RelationColName varchar(100))
Declare @NextTransmissionIDs TABLE(TransmissionID bigint)
Declare @HoldMainName varchar(100)
Declare @SourceServer varchar(max)
Declare @DestinationServer varchar(max)
Declare @vsql varchar(max)
Declare @flag int
Set @SourceServer='[XYZ].[DBName].[dbo]'
Set @DestinationServer='[ABC].[DBName].[dbo]'
INSERT INTO @MainTableNames(TableName)
Values ('A'),('B'),('C'),('D');
IF OBJECT_ID('tempdb..#NextIDs','local') IS NOT NULL
DROP TABLE #NextIDs
CREATE TABLE #NextIDs (
UniqueID varchar(100))
Declare maintablecursor Cursor for
SELECT TableName from @MainTableNames
Declare @MainTabName varchar(100)
Declare @TransTabName varchar(100)
Declare @TransRelationCol bigint
if not exists(select 1 from sys.servers where name='ABC')
begin
EXEC sp_addlinkedserver @server = 'ABC'
print 'Server Added in the list '
end
open maintablecursor
FETCH NEXT from maintablecursor into @MainTabName
Declare @StartDate datetime
Declare @EndDate datetime
SET @StartDate = '2017-07-05 00:00:00.000'
SET @EndDate = '2017-07-05 23:59:59.000'
Select @HoldMainName = @MainTabName
/*Set @vsql=' SELECT 1 FROM '+@SourceServer+'.'+ @MainTabName+' WHERE [dbo].'+@MainTabName+'.[receiptdt] >='+@StartDate+' and [dbo].'+@MainTabName+'.[receiptdt] <= '+@EndDate+' and transmissiontid is not null'
EXEC(@vsql)*/ **used but getting error near receiptdt and while loop**
--while EXISTS (EXEC(@vsql))
--BEGIN
set @vsql=' INSERT INTO #NextIDS(UniqueID) SELECT TOP 1 ID FROM '+@SourceServer+'.'+@MainTabName +
' WHERE convert(datetime, [dbo].'+@MainTabName+'.[receiptdt], 102) <= cast('+ @EndDate+' AS DATETIME) and ID is not null;'
EXEC(@vsql)
Set @vsql =' Insert into '+ @DestinationServer+'.'+@MainTabName+
' SELECT * FROM ' +@SourceServer+'.'+@MainTabName+' AS a
INNER JOIN #NextIDs AS b ON a.transmissiontid = b.UniqueID'
EXEC(@vsql)
print @MainTabName + ' at time of insertion'
FETCH NEXT from maintablecursor into @MainTabName
WHILE @@FETCH_STATUS = 0
BEGIN
SEt @vsql=' Insert into '+ @DestinationServer+'.'+@MainTabName+
' SELECT * FROM ' +@SourceServer+'.'+@MainTabName+' AS a
INNER JOIN #NextIDs AS b ON a.transmissiontid = b.UniqueID'
EXEC(@vsql)
print @MainTabName + ' at time of insertion'
SET @vsql= ' DELETE '+ @SourceServer+'.'+@MainTabName+
' FROM ' + @SourceServer+'.'+@MainTabName+ ' AS a
INNER JOIN #NextIDs AS b ON a.transmissiontid = b.UniqueID'
EXEC(@vsql)
print @MainTabName + ' at time of deletion'
FETCH NEXT from maintablecursor into @MainTabName
END --Cursor END
CLOSE maintablecursor;
DEALLOCATE maintablecursor;
Set @vsql= ' DELETE '+ @SourceServer+'.'+@HoldMainName+
' FROM '+ @SourceServer+'.'+@HoldMainName+ ' AS a
INNER JOIN #NextIDs AS b ON a.transmissiontid = b.UniqueID'
print(@HoldMainName)
-- END --while loop end
This is the whole code that i m using but i m getting error near the receipt date (receiptdt in source and destination table is in datetime format) type conversion error AND also getting the error near the While loop(while(EXEC(@vsql))) case an expression of non-boolena type
I m defining my start and end date in date time format but still getting the type conversion error