将数据类型varchar转换为float附加prc时出错

时间:2016-01-17 19:58:07

标签: sql-server tsql

CREATE TABLE [dbo].[TB_CUSTPARAM](
    [CUSTTYPE] [varchar](10) NOT NULL,
    [PARAMETER] [varchar](20) NULL,
    [DATATYPE] [int] NULL,
    [TEXT_PARAM] [varchar](50) NULL,
    [RANGE_FROM] [int] NULL,
    [RANGE_TO] [int] NULL,
    [RATING] [int] NULL,
    [DESCRIPTION] [varchar](50) NULL,
    [TABLENAME] [varchar](50) NULL,
    [COLUMNNAME] [varchar](100) NULL,
    [SQLQUERY] [varchar](1000) NULL,
    [SRNO] [int] NOT NULL,
    [WEIGHT] [numeric](4, 0) NULL,
    [RANGE_VIEW_NAME] [varchar](200) NULL
) ON [PRIMARY]
-------------------------------------------------------------------------
CREATE TABLE [dbo].[TEMP_CUSTPARAM](
    [CUSTNO] [varchar](50) NOT NULL,
    [RATING] [numeric](18, 0) NULL,
 CONSTRAINT [PK_TEMP_CUSTPARAM] PRIMARY KEY CLUSTERED 
(
    [CUSTNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

-------------------------------------------------------------------
CREATE TABLE [dbo].[TB_CUSTPERSONAL](
    [CUSTNO] [varchar](50) NOT NULL,
    [SEX] [varchar](30) NULL,
    [MARITALSTATUS] [varchar](30) NULL,
    [INCOMESOURCE] [int] NULL,
    [OCCUPATION] [varchar](30) NULL,
    [EMPLOYERSNAME1] [varchar](50) NULL,
    [EMPLOYERPHNO1] [varchar](15) NULL,
    [EMPLOYERADDR1] [varchar](100) NULL,
    [YEARSOFSERVICE1] [numeric](30, 2) NULL,
    [EMPLOYERSNAME2] [varchar](50) NULL,
    [EMPLOYERPHNO2] [varchar](15) NULL,
    [EMPLOYERADDR2] [varchar](100) NULL,
    [YEARSOFSERVICE2] [numeric](30, 2) NULL,
    [TOTALYEAROFSERVICE] [numeric](30, 2) NULL,
    [NOOFDEPENDANTS] [varchar](10) NULL,
    [CURRANNUALINCOME] [numeric](30, 3) NULL,
    [QUALIFICATION] [varchar](30) NULL,
    [DESIGNATION] [varchar](30) NULL,
    [VERIFICATIONTYPE] [int] NULL,
    [PURPOSEOFACCOUNT] [varchar](50) NULL,
    [REFEREDBY] [varchar](50) NULL,
    [ANTICIPATEDACTIVITY] [varchar](50) NULL,
    [NI_NUMBER] [varchar](35) NULL,
    [IDENTIFICATION_MARK] [varchar](50) NULL,
    [PKID] [varchar](50) NULL,
    [TXNTURNOVER] [varchar](50) NULL,
    [MONTHLY_INCOME] [numeric](30, 3) NULL,
    [FATHER_SPOUSE_NAME] [varchar](100) NULL,
    [MOBILENO] [varchar](30) NULL,
    [EMPLOYERTYPE] [varchar](30) NULL,
    [PROFESSIONTYPE] [varchar](30) NULL,
    [BUSINESSTYPE] [varchar](30) NULL,
    [AREAOFOCCUPATION] [varchar](30) NULL,
    [EXPTXNPERQTR] [varchar](30) NULL,
    [TYPEOF4WHEELEROWN] [varchar](30) NULL,
    [NATIONALITY] [varchar](50) NULL,
    [MINORYN] [varchar](1) NULL,
    [SRCITIZENYN] [varchar](1) NULL,
    [FORM60_61YN] [varchar](1) NULL,
    [PASSPORT_NO] [varchar](50) NULL,
    [IF_SELF_EMPLOYED] [varchar](50) NULL,
    [IF_BUSINESS] [varchar](50) NULL,
    [SOURCE_OF_INCOME] [varchar](50) NULL,
    [YOU_LIVE_IN] [varchar](50) NULL,
    [YOUR_RESIDENCE_IS] [varchar](50) NULL,
    [DI_DATE] [datetime] NULL,
    [REC_UPDATEDT] [datetime] NULL,
    [DAILY_INCOME] [varchar](50) NULL,
    [EMPLOYERPHNO] [varchar](50) NULL,
    [EMPLOYERADDR] [varchar](50) NULL,
    [YEARSOFSERVICE] [varchar](50) NULL,
    [FIRSTNAME] [varchar](50) NULL,
    [MIDDLENAME] [varchar](50) NULL,
    [LASTNAME] [varchar](50) NULL,
    [PLACEOFBIRTH] [varchar](50) NULL,
    [PASSPORT_YN] [varchar](50) NULL,
    [EMIRATESID_YN] [varchar](50) NULL,
    [RESIVISA_YN] [varchar](50) NULL,
    [SALCERTIFICATE_YN] [varchar](50) NULL,
    [EMPLOYMENT_STATUS] [varchar](50) NULL,
    [EMPLOYERSNAME] [varchar](50) NULL,
    [E_ADDRESS2] [varchar](50) NULL,
    [E_ADDRESS3] [varchar](50) NULL,
    [E_COUNTRY] [varchar](50) NULL,
    [YEAROFSERVICE] [varchar](50) NULL,
    [DEPENDENT_CHILDREN] [varchar](50) NULL,
    [SPOUSE_EMP_STATUS] [varchar](50) NULL,
    [ACCOMODATION] [varchar](50) NULL,
    [PROFESSION] [varchar](50) NULL,
    [RESVISANO] [varchar](50) NULL,
    [RESVISAISSUEPLACE] [varchar](50) NULL,
    [RESVISAISSUEDATE] [varchar](50) NULL,
    [RESVISAEXPIRYDATE] [varchar](50) NULL,
    [EMP_CODE] [varchar](50) NULL,
    [PREV_EMPLOYER] [varchar](50) NULL,
    [EMP_JOIN_DATE] [varchar](50) NULL,
    [ACCOUNTOPERATEDBY] [varchar](50) NULL,
    [RESIDENT_STATUS] [varchar](50) NULL,
    [CREDIT_CARDNO] [varchar](50) NULL,
    [ISSED_BY] [varchar](50) NULL,
    [EXPIRY_DATE] [varchar](50) NULL,
    [SCHEME_NAME] [varchar](50) NULL,
    [TENURE_SCHEME] [varchar](50) NULL,
    [INSTALLAMT_SCHEME] [varchar](50) NULL,
    [NOOFINSTALL_SCHEME] [varchar](50) NULL,
    [MATURITYAMT_SCHEME] [varchar](50) NULL,
    [MONTHLYAMT_SCHEME] [varchar](50) NULL,
    [TERM_DEPOSIT] [varchar](50) NULL,
    [DATE_OF_ISSUE] [varchar](50) NULL,
    [DATE_OF_MATURITY] [varchar](50) NULL,
    [TERMDEPO_AMOUNT] [varchar](50) NULL,
    [TERMDEPO_RENEWAL] [varchar](50) NULL,
    [TERMDEPO_RATE] [varchar](50) NULL,
    [INTIAL_DEPOSIT] [varchar](50) NULL,
    [SPECIAL_INST] [varchar](50) NULL,
    [NAME_CONTACTPERSON] [varchar](50) NULL,
    [NAME_CONTACTNUMBER] [varchar](50) NULL,
    [MODIFIED_ON] [varchar](50) NULL,
    [MODIFIED_BY] [varchar](50) NULL,
    [MOTHERSMAIDENNAME] [varchar](50) NULL,
    [CUSTOMER_FIRSTNAME] [varchar](50) NULL,
    [CUSTOMER_LASTNAME] [varchar](50) NULL,
    [CUSTOMER_MIDDLENAME] [varchar](50) NULL,
    [EMPLOYERCITY] [varchar](50) NULL,
    [EMPLOYERSTATE] [varchar](50) NULL,
    [EMPLOYERZIP] [varchar](50) NULL,
    [RESIDENTCOUNTRY] [varchar](50) NULL,
    [EMPLOYERCOUNTRYCODE] [varchar](50) NULL,
    [INRODU_NAME] [varchar](50) NULL,
    [RISKCATEGORY] [varchar](50) NULL,
    [ENTITY_GROUP_CODE] [varchar](50) NULL,
    [ACCTNO_INTRO] [varchar](50) NULL,
    [ENTITY_CODE] [varchar](50) NULL,
 CONSTRAINT [PK_TB_CUSTPERSONAL] PRIMARY KEY CLUSTERED 
(
    [CUSTNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
----------------------------------------------------------


create
PROCEDURE    PROC_CUSTRATING_INC
 @WEIGHTAGE FLOAT,
@TAB_NAME VARCHAR(4000), 
@COL_NAME VARCHAR(4000)
--@ENTITY_CODE VARCHAR(50),@ENTITY_GROUP_CODE VARCHAR(50)
AS
BEGIN
DECLARE @v_DAT_ DATETIME,
 @v_DAT2 DATETIME,
 @v_Pro_ VARCHAR(100),
 @v_LAS_ DATETIME,
 @v_SQL_ VARCHAR(500),
 @v_COU_ FLOAT,
 @v_TOT_ FLOAT,
 @v_BAT_ FLOAT,

 @v_SQLSTRING VARCHAR(1000)


SET NOCOUNT ON;

SET @v_DAT_=GETDATE();
SET @v_Pro_='PROC_CUSTRATING_INC';

DELETE FROM TEMP_CUSTPARAM;

 SELECT @v_LAS_ = LASTRUNDT FROM TB_SCHEDULE WHERE PROCESS_ID ='2';
SET  @v_SQLSTRING = 'INSERT INTO /*+ APPEND*/ TEMP_CUSTPARAM(CUSTNO, RATING)
SELECT DISTINCT Q.CUSTNO, ISNULL(((ISNULL(P.RATING,0) * '+ ISNULL(@WEIGHTAGE, '') +')/100),0) AS RATING
FROM '+ ISNULL(@TAB_NAME, '') +' Q INNER JOIN TB_CUSTPARAM P
ON Q.' + ISNULL(@COL_NAME, '') +'=P.TEXT_PARAM
AND P.TABLENAME='''+ ISNULL(@TAB_NAME, '') +''' AND P.COLUMNNAME='''+ ISNULL(@COL_NAME, '') +'''
AND TRUNC(Q.DI_DATE) >= '''+ ISNULL(@v_LAS_, '') +''' ';


PRINT 'v_SQLSTRING111=== ' + ISNULL(@v_SQL_, '');
EXECUTE (@v_SQLSTRING);

-- RETURN;
MERGE INTO TB_RATING_TEMP A
USING TEMP_CUSTPARAM E
ON (A.CUSTNO=E.CUSTNO)
WHEN MATCHED THEN
UPDATE SET
A.TEMP_RATING=ISNULL(E.RATING,0);

SET  @v_SQLSTRING='UPDATE TB_RATING_TEMP A
SET
A.PARAMCOUNT=A.PARAMCOUNT + 1,
A.BRATING = NVL(A.BRATING,0) + NVL(A.TEMP_RATING,0)
WHERE
A.CUSTNO IN
(
SELECT Q.CUSTNO
FROM TB_CUSTPARAM P,'+ ISNULL(@TAB_NAME, '') +' Q
WHERE P.TEXT_PARAM=TO_CHAR(Q.'+ ISNULL(@COL_NAME, '') +')
)';

PRINT 'v_SQLSTRING222=== ' + ISNULL(@v_SQL_, '');
EXECUTE (@v_SQLSTRING);
PRINT 4;
SET @v_DAT2=GETDATE();
--PRINT  'PROCEDURE ' + ISNULL(@v_Pro_, '') + ' TAKES : ' + ISNULL(CONVERT(VARCHAR, trunc((((86400*(@v_DAT2-@v_DAT_))/60)/60)/24)), '') + ' Days ' + ISNULL(CONVERT(VARCHAR,  trunc(((86400*(@v_DAT2-@v_DAT_))/60)/60)- 24*(trunc((((86400*(@v_DAT2-@v_DAT_))/60)/60)/24))), '') + ' Hrs. ' + ISNULL(CONVERT(VARCHAR,  trunc((86400*(@v_DAT2-@v_DAT_))/60)- 60*(trunc(((86400*(@v_DAT2-@v_DAT_))/60)/60)) ), '') + ' Mins.'  + ISNULL(CONVERT(VARCHAR, trunc(86400*(@v_DAT2-@v_DAT_))- 60*ISNULL((trunc((86400*(@v_DAT2-@v_DAT_))/60)), '') + ' Sec.'), '');
  END; 

运行

EXEC PROC_CUSTRATING_INC  11.0,'TB_CUSTPERSONAL','OCCUPATION'

导致错误

  

将数据类型varchar转换为float时出错。

1 个答案:

答案 0 :(得分:0)

我认为问题出现在下面的陈述中

SET  @v_SQLSTRING = 'INSERT INTO /*+ APPEND*/ TEMP_CUSTPARAM(CUSTNO, RATING)
SELECT DISTINCT Q.CUSTNO, ISNULL(((ISNULL(P.RATING,0) * '+ ISNULL(@WEIGHTAGE,     '') +')/100),0) AS RATING
FROM '+ ISNULL(@TAB_NAME, '') +' Q INNER JOIN TB_CUSTPARAM P
ON Q.' + ISNULL(@COL_NAME, '') +'=P.TEXT_PARAM
AND P.TABLENAME='''+ ISNULL(@TAB_NAME, '') +''' AND P.COLUMNNAME='''+     ISNULL(@COL_NAME, '') +'''
AND TRUNC(Q.DI_DATE) >= '''+ ISNULL(@v_LAS_, '') +''' ';

ISNULL(@WEIGHTAGE, '')应更改为ISNULL(@WEIGHTAGE, 0.0),因为@WEIGHTAGE是float,您正尝试将varchar ''更改为此float导致它失败的ISNULL语句。