Solved

Inner Joining two @table_variables

Posted on 2011-09-09
4
259 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard 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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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