Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inner Joining two @table_variables

Posted on 2011-09-09
4
Medium Priority
?
265 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 2000 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

927 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