[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Adding a field to a temp table

Posted on 2008-10-06
1
Medium Priority
?
174 Views
Last Modified: 2012-05-05
I am using the following code, see attached code in my query to convert an image to rtf.

At present the two fields outputted are applicantid, RTF

I want to add another field to the output, but cannot seem to be able to do so.

The field is p.surname which is of varchar(20)

i.e.

DECLARE YourCursor CURSOR
        FOR SELECT  c.applicantid, p.surname                    
            FROM    applicant a
                    LEFT OUTER JOIN coverletter c ON a.applicantid = c.applicantid
                    LEFT OUTER JOIN PersonalInformation p ON a.ApplicantID = p.ApplicantID
            WHERE   c.date_dt >= @start and c.date_dt < @end
           
Can anybody help?
IF OBJECT_ID('tempdb.dbo.#Temp') IS NOT NULL 
   DROP TABLE #Temp
 
DECLARE @start DATETIME
DECLARE @end DATETIME
 
 
SET @start = DATEADD(day, -67, CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
SET @end = DATEADD(day, 1, @start)
 
DECLARE @TxtPtr BINARY(16),
        @applicantid INTEGER,
        @Data VARCHAR(8000),
        @Offset INTEGER
SET              NOCOUNT ON
CREATE TABLE #Temp
       (
         applicantid INTEGER,
         RTF TEXT
       )
DECLARE YourCursor CURSOR
        FOR SELECT  c.applicantid                    
            FROM    applicant a
                    LEFT OUTER JOIN coverletter c ON a.applicantid = c.applicantid
                    LEFT OUTER JOIN PersonalInformation p ON a.ApplicantID = p.ApplicantID
            WHERE   c.date_dt >= @start and c.date_dt < @end
OPEN YourCursor
FETCH NEXT FROM YourCursor INTO @applicantid
WHILE @@FETCH_STATUS = 0
      BEGIN
            INSERT  #Temp ( applicantid, RTF )
            VALUES  ( @applicantid, '' )
            SELECT  @TxtPtr = TEXTPTR(RTF),
                    @OffSet = 0
            FROM    #Temp
            WHERE   applicantid = @applicantid
            WHILE @OffSet IS NOT NULL
                  BEGIN
                        SELECT  @Data = CAST(CAST(SUBSTRING(document_im, @Offset + 1, 8000) AS VARBINARY(8000)) AS VARCHAR(8000))
                        FROM    coverletter
                        WHERE   applicantid = @applicantid
                        IF LEN(@Data) > 0 
                           BEGIN
                                 UPDATETEXT #Temp.RTF @TxtPtr @Offset NULL @Data
                                 SET @OffSet = @OffSet + 8000
                           END
                        ELSE 
                           BEGIN
                                 SET @OffSet = NULL
                           END
                  END
            FETCH NEXT FROM YourCursor INTO @applicantid
      END
CLOSE YourCursor
DEALLOCATE YourCursor
SELECT  applicantid,
        RTF
FROM    #Temp
DROP TABLE #Temp

Open in new window

0
Comment
Question by:halifaxman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 22649957
Not sure what you're needing to DO w/ surname once you get it ... but this should get it into the table for you.

--http://www.experts-exchange.com/Q_23790211.html
 
IF OBJECT_ID('tempdb.dbo.#Temp') IS NOT NULL 
   DROP TABLE #Temp
 
DECLARE @start DATETIME
DECLARE @end DATETIME
 
 
SET @start = DATEADD(day, -67, CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
SET @end = DATEADD(day, 1, @start)
 
DECLARE @TxtPtr BINARY(16),
        @applicantid INTEGER,
		@surname varchar(20),
        @Data VARCHAR(8000),
        @Offset INTEGER
SET              NOCOUNT ON
CREATE TABLE #Temp
       (
         applicantid INTEGER,
		surname varchar(20),
         RTF TEXT
       )
DECLARE YourCursor CURSOR
        FOR SELECT  c.applicantid , p.surname                    
            FROM    applicant a
                    LEFT OUTER JOIN coverletter c ON a.applicantid = c.applicantid
                    LEFT OUTER JOIN PersonalInformation p ON a.ApplicantID = p.ApplicantID
            WHERE   c.date_dt >= @start and c.date_dt < @end
OPEN YourCursor
FETCH NEXT FROM YourCursor INTO @applicantid, @Surname
WHILE @@FETCH_STATUS = 0
      BEGIN
            INSERT  #Temp ( applicantid, surname, RTF )
            VALUES  ( @applicantid, @surname, '' )
            SELECT  @TxtPtr = TEXTPTR(RTF),
                    @OffSet = 0
            FROM    #Temp
            WHERE   applicantid = @applicantid
            WHILE @OffSet IS NOT NULL
                  BEGIN
                        SELECT  @Data = CAST(CAST(SUBSTRING(document_im, @Offset + 1, 8000) AS VARBINARY(8000)) AS VARCHAR(8000))
                        FROM    coverletter
                        WHERE   applicantid = @applicantid
                        IF LEN(@Data) > 0 
                           BEGIN
                                 UPDATETEXT #Temp.RTF @TxtPtr @Offset NULL @Data
                                 SET @OffSet = @OffSet + 8000
                           END
                        ELSE 
                           BEGIN
                                 SET @OffSet = NULL
                           END
                  END
            FETCH NEXT FROM YourCursor INTO @applicantid, @Surname
      END
CLOSE YourCursor
DEALLOCATE YourCursor
SELECT  applicantid, surname,
        RTF
FROM    #Temp
DROP TABLE #Temp

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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