Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

invoke-sqlcmd : Error converting data type varchar to numeric

Hello,

I try to collect dbspace with ps script :

-The table ddl and the ps script are  :

CREATE TABLE [dbo].[DSpace](
	[SERVERNAME] [varchar](50) NULL,
	[DatabaseName] [varchar](128) NULL,
	[Log_filename] [varchar](128) NULL,
	[Log_filesize] [decimal](18, 2) NULL,
	[Used_space] [decimal](18, 2) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

    $SQLInstance = "SQLTEST"

    $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $SQLInstance

    $DBStats = $srv.Databases
		

        foreach ($DB in $DBStats) {
		IF ( $DB.status -eq "Normal" ) {
		$DBName = $DB.Name
		$db.get_logfiles() | % { New-Object PsObject -Property @{
		'Log File' = $_.FileName
		'Size (MB)' = [math]::round($_.Size/1KB,2)
		'Used Space (MB)' = [math]::round($_.UsedSpace/1KB,2)
		} | tee -Variable vals | Format-Table -auto
		
		$val.{Log File} 
		$val.{Size (MB)} 
		$val.{Used Space (MB)} 
	
		}
        $InsertResults = @"
   
		INSERT INTO DSpace (SERVERNAME ,DatabaseName ,Log_filename ,Log_filesize ,Used_space)
		VALUES ('$SQLInstance', '$DBName', '$val.{Log File}', '$val.{Size (MB)}', '$val.{Used Space (MB)}')
		
		"@
		

        invoke-sqlcmd @params -Query $InsertResults

		}
	}

Open in new window

The error returned is :
invoke-sqlcmd : Error converting data type varchar to numeric.
At line:20 char:9
+         invoke-sqlcmd @params -Query $InsertResults
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

How can I resolve this problem ?

Thanks
Avatar of Qlemo
Qlemo
Flag of Germany image

Remove the single quotes inside the INSERT statement for all numeric values, and you need to make them a subexpression inside of double quotes.
But there is another issue - if $DB.status is not "Normal", you still try to insert values, which are not recent by then - the INSERT belongs into the IF block.
And there is a typo in the tee-object, so the wrong variable is filled.
CREATE TABLE [dbo].[DSpace](
	[SERVERNAME] [varchar](50) NULL,
	[DatabaseName] [varchar](128) NULL,
	[Log_filename] [varchar](128) NULL,
	[Log_filesize] [decimal](18, 2) NULL,
	[Used_space] [decimal](18, 2) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

$SQLInstance = "SQLTEST"

$srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $SQLInstance

$DBStats = $srv.Databases
		

foreach ($DB in $DBStats) {
  IF ( $DB.status -eq "Normal" ) {
    $DBName = $DB.Name
    $db.get_logfiles() | % { New-Object PsObject -Property @{
      'Log File' = $_.FileName
      'Size (MB)' = [math]::round($_.Size/1KB,2)
      'Used Space (MB)' = [math]::round($_.UsedSpace/1KB,2)
    } | tee -Variable val | Format-Table -auto

    $InsertResults = @"
      INSERT INTO DSpace (SERVERNAME    , DatabaseName, Log_filename ,Log_filesize ,Used_space)
                  VALUES ('$SQLInstance', '$DBName'   , '$($val.{Log File})', $($val.{Size (MB)}), $($val.{Used Space (MB)}))	
"@
    invoke-sqlcmd @params -Query $InsertResults
  }
}

Open in new window

In your code you wrote the following:
tee -Variable vals

Is that val instead?
Avatar of bibi92

ASKER

For qlemo, thanks :
invoke-sqlcmd : Incorrect syntax near ','.
At line:1 char:1
+ invoke-sqlcmd @params -Query $InsertResults
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Can't find any issue. Please add this before or after line 34 for debugging:
Write-Host $InsertResults

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibi92

ASKER

ok i will test tomorrow. Thanks