Solved

Inner Joining two @table_variables

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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