将nvarchar值转换为int时转换失败

时间:2015-03-23 15:15:17

标签: sql sql-server

DECLARE @Scripts TABLE (
                Id INT  IDENTITY(1, 1) PRIMARY KEY,
                Script NVARCHAR(4000)
            )

Insert into @Scripts(Script)
select 'Exec POI2Version7.dbo.MatchFirstName @FirstNameFromUser =''' 
+ @firstname + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
     UNION All

依旧......

当我在插入数据时创建了Id作为标识列并指定了Script列时,该字符串应仅插入Script列。

但我认为它试图将其插入Id列。 我收到以下错误:

Msg 245, Level 16, State 1, Procedure CallingProcedure, Line 153
Conversion failed when converting the nvarchar value 'Exec POI2Version7.dbo.MatchFirstName 
@FirstNameFromUser ='Simri', @checkbool = 0' to data type int.

我的代码中出现了什么问题?

编辑:

Rest of the query looks like this :

Insert into @Scripts(Script)
select 'Exec POI2Version7.dbo.MatchFirstName @FirstNameFromUser =''' + @firstname + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
     UNION All
 select 'Exec POI2Version7.dbo.MatchMiddleName @MiddleNameFromUser=''' + @MiddleName + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
    UNION ALL
 select  'Exec POI2Version7.dbo.MatchLastName @LastNameFromUser ''' + @Lastname + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
     UNION ALL
 select 'Exec POI2Version7.dbo.MatchDate @DateFromUser = ''' + @date + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
      UNION ALL
select 'Exec POI2Version7.dbo.MatchMonth @MonthFromUser =''' + @month + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
      UNION ALL
select 'Exec POI2Version7.dbo.MatchYear @YearFromUser = ''' + @year + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
      UNION ALL
 select 'Exec POI2Version7.dbo.MatchSSN1 @ssnfromuser = ''' + @SSN1 + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
     UNION ALL
select 'Exec POI2Version7.dbo.MatchSSN2 @ssnfromuser =''' + @SSN2 + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
      UNION ALL
select 'Exec POI2Version7.dbo.MatchSSN3 @ssnfromuser =''' + @SSN3 + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
   UNION All
select 'Exec POI2Version7.dbo.MatchZIP @ZIPfromuser =''' + @ZIP + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
UNION ALL
select 'Exec POI2Version7.dbo.MatchState @StateFromUsers = ''' + @StateOfResidence + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 UNION ALL
select 'Exec POI2Version7.dbo.MatchCity @CityfromUser = ''' + @City + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.MatchStreetName @StreetNameFromUser =''' + @Streetname + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
UNION ALL
select 'Exec POI2Version7.dbo.MatchStreetType @StreetTypeFromUser = ''' + @StreetType + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
UNION ALL
select 'Exec POI2Version7.dbo.MatchBuildingNumber @BuildingNumberfromUser = ''' + @BuildingNumber + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
  UNION ALL
select 'Exec POI2Version7.dbo.dbo.MatchAptNumber @AptNumberFromUser =''' + @Aptnumber + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
UNION ALL
select 'Exec POI2Version7.dbo.MatchPatientMaidenLN  @PatientMaidenLN =''' + @patientMaidenLN + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
UNION ALL
select 'Exec POI2Version7.dbo.MatchFatherFirstName @FatherFN =''' + @FatherFN  + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 Union ALL
select 'Exec POI2Version7.dbo.MatchFathersLastName @FatherLN =''' + @FatherLN + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 Union All
select 'Exec POI2Version7.dbo.MatchMotherFirstName @MOTHERFN =''' + @MotherFN + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
  Union All
select 'Exec POI2Version7.dbo.MatchMothersLastName @MOtherLN = ''' + @MotherLN + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 Union All 
select 'Exec POI2Version7.dbo.MatchMothersMaidenLN @MotherMaidenLN =''' + @mothermaidenLN + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
 select 'Exec POI2Version7.dbo.MatchpatientNickName @patientNickName  =''' + @patientNickName + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.MatchCitizenshp  @citizenship =''' + @Citizenship + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All 
select 'Exec POI2Version7.dbo.MatchNationality  @nationality =''' + @Nationality + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.MatchEthinicity @ethnicity= ''' + @ethnicity + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All  
select 'Exec POI2Version7.dbo.MatchRace @race =''' + @race + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 Union All
select 'Exec POI2Version7.dbo.MatchReligion @religion = =''' + @Religion + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.MatchPrimaryLanguage  @primarylanguage = ''' + @primarylanguage + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
 Union All
select 'Exec POI2Version7.dbo.Matchpatientmrn @patientmrn =  ''' + @patientmrn + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
   Union All
 select 'Exec POI2Version7.dbo.Matchhospitalname  @hospitalname = =''' + @hospitalname + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.MatchMedicaidid @medicaidid = =''' + @medicaidid + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.Matchpcpnpi @pcpnpi = ''' + @pcpnpi + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union All
select 'Exec POI2Version7.dbo.Matchphonenumber @phonenumber = ''' + @phonenumber + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))
Union ALL
 select 'Exec POI2Version7.dbo.Matchemail @email = ''' + @email + ''', @checkbool = ' + cast(@checkbool as nvarchar(10))

0 个答案:

没有答案