Solved

TSQL put recordset into new table for mail merge

Posted on 2012-03-22
4
452 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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