Solved

Inner Joining two @table_variables

Posted on 2011-09-09
4
256 Views
Last Modified: 2012-05-12
The two @table_variables given below are working fine individually. I want to inner join the two table_variables. I decalred primary keys also.

I am getting the following errors.

The definition for column 'PrimaryKey' must include a data type.
The definition for column 'PrimaryKey' must include a data type.
Must declare the table variable "@tblPrevMonthSavings".

How to fix it and get the result set using inner join.

DECLARE @tblPrevMonth TABLE(
   MbrNo char(10),
   ID INT,
   Balance MONEY,
   PrimaryKey(MbrNo,ID)
);

DECLARE @sql VARCHAR(2000);
SET @sql = '
SELECT MbrNo,ID,Balance  
FROM SSQLARCHIVE_' + DATENAME(YY, DATEADD(MM, -1, GETDATE())) + '.dbo.t_accounthistory_' + RIGHT('00'+CONVERT(CHAR(2), MONTH(DATEADD(MM, -1, GETDATE()))), 2) +
' WHERE (BALANCE / 100 < 0) AND (ID < ''50'')';
PRINT(@sql);

INSERT INTO @tblPrevMonth(MbrNo,ID,Balance)
EXEC sp_executesql @sql
;

Declare @tblPreMonthSavings Table
(
AccountNo  char(10),
Name varchar(255),
Address varchar(255),
PID  char(2),
SNumber  int,
PDate  datetime,
UserName varchar(100),
TDescription varchar(200),
ACode char(1),
SCode char(1),
Description char(16),
Amount money,
NBalance money,
FComments varchar(800),
Branch smallint,
EDate smalldatetime,
PrimaryKey(AccountNo,PID,SNumber)
);

Declare @ssql nvarchar(4000);
SET @ssql= '
SELECT AccountNo,dbo.fn_GetDetails(AccountNo, "FULL", "P") AS Name,
dbo.fn_GetDetails( AccountNo, "ADDR", "P") AS Address,PID,SNUMBER,PDATE,UserName,TDESCRIPTION, ISNULL(ACTIONCODE, "") AS ACODE,
                      ISNULL( SOURCECODE, "") AS SCODE,DESCRIPTION,AMOUNT,NBALANCE, FCOMMENTS,BRANCH,EDATE

FROM SYMSQLARCHIVE_' + DATENAME(YY, DATEADD(MM, -1, GETDATE())) + '.dbo.t_SavingsHistory_' + RIGHT('00'+CONVERT(varchar(2), MONTH(DATEADD(MM, -1, GETDATE()))), 2) +
' WHERE (ISNULL( ACODE, "") <> "c") AND ( NBALANCE < 0)AND (AMOUNT < 0) ';
PRINT(@ssql);
INSERT INTO @tblPreMonthSavings(PAccount,Name,Address,PID,SNumber,PDate,UserName,TDescription,ACode,SCode,Description,Amount,NBalance,FComments,Branch,EDate)
EXEC sp_executesql @ssql


;

Select AccountNo,Name,Address,PID,SNumber,PDate,UserName,TDescription,ACode,SCode,
Description ,Amount,NBalance,FComments,Branch,EDate,Balance

From @tblPreMonthSavings PS Inner join @tblPrevMonth PM on
  PS.AccountNo =  PM.MbrNo  AND
                  PS.PID  =  PM.ID ;


Thanks
0
Comment
Question by:KavyaVS
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
JestersGrind earned 500 total points
ID: 36510663
When you define the table variable primary key should be two words like this.

DECLARE @tblPrevMonth TABLE(
   MbrNo char(10),
   ID INT,
   Balance MONEY,
   PRIMARY KEY(MbrNo,ID));

Greg

0
 

Author Comment

by:KavyaVS
ID: 36511903
I am getting this error.

Must declare the table variable "@tblPrevMonthSavings".

But I declared the table variable "@tblPrevMonthSavings"

Why I am getting it.

Thanks
0
 
LVL 21

Assisted Solution

by:JestersGrind
JestersGrind earned 500 total points
ID: 36512161
Looking at your code, you have @tblPreMonthSavings without the v not @tblPrevMonthSavings.  That could be it.

Greg

0
 

Author Closing Comment

by:KavyaVS
ID: 36513063
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

24 Experts available now in Live!

Get 1:1 Help Now