Solved

TSQL put recordset into new table for mail merge

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 32
convert null in sql server 12 34
Return 0 on SQL count 24 30
Merge two rows in SQL 4 17
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
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 the fundamental information of how to create a table.

831 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