Solved

TSQL put recordset into new table for mail merge

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

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

747 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

14 Experts available now in Live!

Get 1:1 Help Now