• Hi this is the correct piece of code. I missed AS BEGIN in view definition.

    DECLARE @sql varchar(max)

    SET @sql='

    USE '+@Database1+'

    IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME =''test_view'')

    BEGIN

    DROP VIEW test_view

    PRINT ''VIEW EXISTS''

    END'

    PRINT @sql

    EXEC(@sql)

    SET @sql=''

    SET @sql='USE '+@Database1+'

    GO

    CREATE VIEW test_view

    AS

    BEGIN

    SELECT TOP 50 * FROM TEST_TABLE

    '

    PRINT @sql

    EXEC(@SQL)

    I am getting error "Incorrect Syntax near GO"