Q:

Change where statement to exclude duplicates

I have a stored procedure that I use to import orders into a survey software's database. Currently it imports all the orders from the previous day, but recent changes in business plan requires these surveys to be sent out hourly.  The imports come from a view in a different database. attribute 1 is the order number, which will be unique per survey so we can use that to limit the imports.
How do I change this to not pull in duplicates?  
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

Insert INTO SurveyTable
(SurveyTable.firstname,
SurveyTable.token,
SurveyTable.email,
SurveyTable.emailstatus, 
SurveyTable.language, 
SurveyTable.remindersent, 
SurveyTable.attribute_1,
SurveyTable.attribute_2)

select 
    location,
    cast( '' as xml ).value('xs:base64Binary(sql:column( "token" ) )', 'nvarchar(MAX)' ),
    email,
    emailstatus, 
    [language],
    remindersent,
    attribute_1,
    attribute_2
from 
    (
        select 
            RTRIM([Closed_Orders_For_Survey].[Location]) location,
            crypt_gen_random(12) as token, 
            [Closed_Orders_For_Survey].[Email] email,
            'OK' emailstatus,
            'en' [language], 
            'N' remindersent, 
           [Closed_Orders_For_Survey].[Order Number] attribute_1, 
            CONVERT(VARCHAR(10), [Closed_Orders_For_Survey].[Invoice Date],110) attribute_2 
        from 
            [Closed_Orders_For_Survey]
        where 
            [Closed_Orders_For_Survey].[Order Date] >= dateadd(DAY, -1, Convert(date, GETDATE()))
    ) as x
END

P.S. the token is a generated unique string used in creaating the survey URL. We decided not to use the order number as the token because this would be too predictable and enable people to change their url to fill out other users surveys. 

A:

This is not perfectly optimal for very large data sets (you'd want to make sure you have good indexes on attribute_1), but you can do a 'where not exists' clause to filter out any orders from the inner query that already have been inserted:
select 
    location,
    cast( '' as xml ).value('xs:base64Binary(sql:column( "token" ) )', 'nvarchar(MAX)' ),
    email,
    emailstatus, 
    [language],
    remindersent,
    attribute_1,
    attribute_2
from 
    (
        select 
            RTRIM([Closed_Orders_For_Survey].[Location]) location,
            crypt_gen_random(12) as token, 
            [Closed_Orders_For_Survey].[Email] email,
            'OK' emailstatus,
            'en' [language], 
            'N' remindersent, 
           [Closed_Orders_For_Survey].[Order Number] attribute_1, 
            CONVERT(VARCHAR(10), [Closed_Orders_For_Survey].[Invoice Date],110) attribute_2 
        from 
            [Closed_Orders_For_Survey]
        where 
            [Closed_Orders_For_Survey].[Order Date] >= dateadd(DAY, -1, Convert(date, GETDATE()))
    ) as x
where 
    not exists (
        select attribute_1 
            from SurveyTable 
            where 
                attribute_1 = x.attribute_1
        )

