Solved

invoke-sqlcmd : Error converting data type varchar to numeric

Posted on 2014-11-12
6
265 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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
In your code you wrote the following:
tee -Variable vals

Is that val instead?
0
 

Author Comment

by:bibi92
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 68

Expert Comment

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

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
ok i will test tomorrow. Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now