Solved

TSQL put recordset into new table for mail merge

Posted on 2012-03-22
4
453 Views
Last Modified: 2012-03-22
Hi All,

I need some help with TSQL. I want to run the following on a remote SQL. It currently works and correctly returns the data. However, I want it to write the recordset to a new table 'Temp_MailMerge_Table' . AS the name implies, its so as I can use it for a mail merge in WORD 2007.

---
SELECT us.User_ID
  , us.User_UN
  , us.User_PW
  , us.User_Email
  , pa.Parnets_ParentNames

FROM Tbl_Users us

Left JOIN Tbl_Parents pa ON
us.User_ID = pa.Parents_UserID

WHERE us.User_Type = 3


'NOT sure how this bit is written in TSQ

1 - Empty Temp_MailMerge_Table
2 - PUT this data into Temp_MailMerge_Table
0
Comment
Question by:Abiel de Groot
[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
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:appari
appari earned 100 total points
ID: 37751195
1.Empty Temp_MailMerge_Table
delete from Temp_MailMerge_Table;

2 - PUT this data into Temp_MailMerge_Table

insert into Temp_MailMerge_Table
SELECT us.User_ID
  , us.User_UN
  , us.User_PW
  , us.User_Email
  , pa.Parnets_ParentNames

FROM Tbl_Users us

Left JOIN Tbl_Parents pa ON
us.User_ID = pa.Parents_UserID

WHERE us.User_Type = 3
I assumed Temp_MailMerge_Table has same number of fields and in that order as returned by the select sql.
0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 37751251
Hi Appari,

Many thanks, but I get an error.

------------------
Mens. 213, Nivel 16, Estado 1, Línea 3
El nombre de columna o los valores especificados no corresponden a la definición de la tabla.
---------
Column names or specified values do not correspond.


The columns with the TEMP table are in the order of the select statment and the data types are corresponding. However they have different names. I have amended the statement as follows but still get the same error.

Amy advice?

The 'AS' changes it to the actual column name in the TEMP table.

---------------------

delete from Temp_MailMerge_Table;

insert into Temp_MailMerge_Table
SELECT us.User_ID AS Temp_User_ID
  , us.User_UN AS Temp_User_UN
  , us.User_PW AS Temp_User_PW
  , us.User_Email AS Temp_User_Email
  , pa.Parnets_ParentNames AS Temp_Parnets_ParentNames

FROM Tbl_Users us

Left JOIN Tbl_Parents pa ON
us.User_ID = pa.Parents_UserID

WHERE us.User_Type = 3
0
 
LVL 25

Accepted Solution

by:
jogos earned 400 total points
ID: 37751311
Best way to not have problems is to give the column-list.
insert into Temp_MailMerge_Table ( Temp_User_ID
  , Temp_User_UN
  ,  Temp_User_PW
  , Temp_User_Email
  , Temp_Parnets_ParentNames)
SELECT us.User_ID AS Temp_User_ID
  , us.User_UN AS Temp_User_UN
  , us.User_PW AS Temp_User_PW
  , us.User_Email AS Temp_User_Email
  , pa.Parnets_ParentNames AS Temp_Parnets_ParentNames

FROM Tbl_Users us

Left JOIN Tbl_Parents pa ON
us.User_ID = pa.Parents_UserID

WHERE us.User_Type = 3

Open in new window

My SQL is better as my spanish but please also look at the values itself and the definition of your temp table
 
"Server: Msg 213, Level 16, State 5, Line 1
Insert Error: Column name or number of supplied values
does not match table definition."

You can test this by let your select-statement create automaticly a temp-table (test_tabledef)  and compare that definition with that of your Temp_MailMerge_Table
SELECT us.User_ID AS Temp_User_ID
  , us.User_UN AS Temp_User_UN
  , us.User_PW AS Temp_User_PW
  , us.User_Email AS Temp_User_Email
  , pa.Parnets_ParentNames AS Temp_Parnets_ParentNames

into test_tabledef

FROM Tbl_Users us

Left JOIN Tbl_Parents pa ON
us.User_ID = pa.Parents_UserID

WHERE us.User_Type = 3

Open in new window

0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
ID: 37751631
A masterclass ... many thanks guys

A.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

739 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