Hello,
I have written a query in SQL which works fine and i get the desired output. But when i try to use the same in windows powershell i get the following error :
Invoke-Sqlcmd : Invalid length parameter passed to the LEFT or SUBSTRING function.
At line:1 char:11
+ $result = Invoke-Sqlcmd -Query "declare @var int
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Please find below my sql query :
declare @var int
set @var = (select top 1 logid from com_logline
where text like '%COMMON_MSG%' order by logid desc)
declare @temp varchar(max)
declare @print varchar(max)
declare MsgError_cursor CURSOR
for SELECT SUBSTRING(text, CHARINDEX ('SMTP:', text)+4, CHARINDEX (':$:L', text)-CHARINDEX ('SMTP:', text)-4)
FROM com_logline where logid = 41
and text like '%COMMON_MSG_SEND_ERROR%'
order by lstseqno desc
open MsgError_cursor
fetch next from MsgError_cursor into @temp
WHILE @@FETCH_STATUS = 0
BEGIN
set @print = (SELECT REPLACE((@temp),':$',(char(13) + char(10))));
insert into #temp_emailError values (@print)
FETCH NEXT FROM MsgError_cursor into @temp
END
DEALLOCATE MsgError_cursor
Please note, I have even tried using a temp table but to no effect. Any help would be much appreciated.Thanks in advance