Solved

select Distinct  error 8163

Posted on 2004-07-30
7
535 Views
Last Modified: 2008-02-01
     insert into mydatabase..mail_queue ( mailTo, mailType, mailFrom, subject, contentType, mailAux, mailFK, mailFK2, message)
      select  distinct emailAddress, 'regenschirm', @fromAddress, @subject, 'text/html',

Why is this?  It worked fine on SQL SRV 7 but on SQL SERVER 2000 this no longer works!

0
Comment
Question by:bionicblakey
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11682077
You probably had message as VarChar on SQL7 and as Text on SQL2000, or did not use distinct. Try replacing message with cast(message as varchar(8000)) in your select. If that does not work (messages longer than 8000 characters), you will have to use an intermediate step.

(Why is the distinct there?)

0
 

Author Comment

by:bionicblakey
ID: 11682100
distinct is there so that only one email is sent to a user that may appear more than once inthe db.

The code you see above is the exact same code as i used in sql 7.  Also i copied the exact db to sql 2000

I am not sure what you mean, here is the whole Sp

insert into ourdb..mail_queue ( mailTo, mailType, mailFrom, subject, contentType, mailAux, mailFK, mailFK2, message)
      select  distinct emailAddress, 'regenschirm', @fromAddress, @subject, 'text/html',
            cast(contact_emails.contactID as varchar(8000)), @jobID, @template, null
      from CONTACT_EMAILS join RESULTS on contact_emails.contactID = result
            join CONTACTS on RESULTS.result = CONTACTS.ID
            join GROUP_MEMBERS as GM on GM.contactID = contacts.ID
            join GROUP_REQUIREMENTS as GR on GR.groupID = GM.groupID
            join GROUP_TOKENS as GT on GT.tokenCode = GR.tokenCode
            join GROUP_MEMBERS as U on U.groupID = GT.groupID
      where contact_emails.preferred = 'Y' and contacts.useEmail = 'Y'
            and RESULTS.contactUser = @contactUser
            and (GR.actionCode = 'SEND' or GR.actionCode = 'SUPERSEND')
            and U.contactID = @contactUser
      
0
 
LVL 18

Accepted Solution

by:
SjoerdVerweij earned 500 total points
ID: 11682127
Hmm. One of the @vars must be of Text type then. But, there's a way around that:

insert into ourdb..mail_queue ( mailTo, mailType, mailFrom, subject, contentType, mailAux, mailFK, mailFK2, message)
     select emailAddress, 'regenschirm', @fromAddress, @subject, 'text/html',
          x.contactID, @jobID, @template, null
     from (select distinct emailaddress, contact_emails.contactID
     from CONTACT_EMAILS join RESULTS on contact_emails.contactID = result
          join CONTACTS on RESULTS.result = CONTACTS.ID
          join GROUP_MEMBERS as GM on GM.contactID = contacts.ID
          join GROUP_REQUIREMENTS as GR on GR.groupID = GM.groupID
          join GROUP_TOKENS as GT on GT.tokenCode = GR.tokenCode
          join GROUP_MEMBERS as U on U.groupID = GT.groupID
     where contact_emails.preferred = 'Y' and contacts.useEmail = 'Y'
          and RESULTS.contactUser = @contactUser
          and (GR.actionCode = 'SEND' or GR.actionCode = 'SUPERSEND')
          and U.contactID = @contactUser) as x
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11682151
>>I am not sure what you mean, here is the whole Sp<<
As SjoerdVerweij has pointed out you cannot have text, ntext or image in a DISTINCT query (in any version of SQL Server).  That means that one of the columns in the Select statement is one of those data types.  I am going to go out on a limb here and guess that it is "emailAddress"
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11682154
>>One of the @vars must be of Text type then. <<
Good point, I had not thought of that.
0
 

Author Comment

by:bionicblakey
ID: 11682192
i wish it were the emailaddress. but no, it is not.

BSJ
0
 

Author Comment

by:bionicblakey
ID: 11682249
acperkins :   This exact SP worked fine for 3 years using SQL SRV 7. Only until we migrated to SQL 2000 did this error occur.


 SjoerdVerweij : Thanks so much.  That did the trick!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now