Solved

invoke-sqlcmd : Error converting data type varchar to numeric

Posted on 2014-11-12
6
289 Views
Last Modified: 2014-12-08
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
0
Comment
Question by:bibi92
  • 3
  • 2
6 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40438309
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

0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 40438311
In your code you wrote the following:
tee -Variable vals

Is that val instead?
0
 

Author Comment

by:bibi92
ID: 40439454
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
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 69

Expert Comment

by:Qlemo
ID: 40439807
Can't find any issue. Please add this before or after line 34 for debugging:
Write-Host $InsertResults

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40439886
There is a syntax error in my code - a closing curly bracket missing. But that should have errored out pre-execution anything ...
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

I've left the Format-Table as-is but it isn't what I would use - it is called for each line individually, insteadl of all the results together. On the other hand, if I change that, you won't see any intermediate results unless script execution is completed.
0
 

Author Comment

by:bibi92
ID: 40460942
ok i will test tomorrow. Thanks
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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