Solved

Inner Joining two @table_variables

Posted on 2011-09-09
4
255 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
Comment Utility
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
Comment Utility
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
Comment Utility
Looking at your code, you have @tblPreMonthSavings without the v not @tblPrevMonthSavings.  That could be it.

Greg

0
 

Author Closing Comment

by:KavyaVS
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

15 Experts available now in Live!

Get 1:1 Help Now