I am trying to create an Excel spreadsheet that will run a run some SQL code which I have done in the past for simple select statements but now with some more advance code I keep getting an error when I try and run the SQL Server stored procedure from Excel even though it runs fine in SQL Server Management Studio.
The code of the stored procedure is:
truncate table LAS_RPT_IST;
insert into LAS_RPT_IST (IST_Ref_no, IST_Inv_no, LDC_ACCT_ID, IST_Commodity, IST_Tax, IST_STRS, START_DT, END_DT)
(Select
th.user1_tx, th.user4_tx, ldc.ldc_acct_id,
sum(case when th.trans_sub_ty_cd = 'ISTC' then th.trans_am end) as ISTC,
sum(case when th.trans_sub_ty_cd = 'TAX' then th.trans_am end) as TAX,
sum(case when th.trans_sub_ty_cd = 'STRS' then th.trans_am end) as STRS,
th.generic1_dt, th.generic2_dt
from
transaction_history th, ldc_Account ldc
where
th.trans_creation_dt > = Convert(varchar(8),DateAdd(d, -1, Convert(datetime, N'20140930', 101)), 112)
and th.user10_tx = 'IST'
and th.ldc_acct_id = ldc.ldc_acct_id
group by
th.user1_tx, th.user4_tx, ldc.ldc_acct_id, th.generic1_dt, th.generic2_dt );
But when I put this into Excel and try and run it I get this error
I can run select statements against the DB from Excel with no issue.
Can anyone tell me what I am missing?
You'll have to run the TRUNCATE and INSERT operations in seperate commands.
Related
I am importing a stored procedure from SQL Server 2008 into Excel 2016, and get an error when I execute it in Excel:
Error converting data type nvarchar to date
This is the stored procedure [LA_VOTER].[Temp].[dmv_import]:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Arash B
-- Create date: 6/5/2018
-- Description: test for creating dynamic 'Data Entry Signature Verification' worksheeet
-- =============================================
ALTER PROCEDURE [Temp].[dmv_import]
#date DATE = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE #date_work DATE
IF #date IS NULL
SET #date_work = GETDATE()
ELSE
SET #date_work = #date
/*count of processed*/
SELECT
vi.import_code as 'DMV Category',
SUM(CASE WHEN CONVERT(date, vi.createdate, 101) = #date_work THEN 1 ELSE 0 END) AS 'received',
SUM(CASE WHEN CONVERT(date, vi.processed_date, 101) = #date_work THEN 1 ELSE 0 END) AS 'processed'
FROM
DIMSNet.dbo.voter_import vi
WHERE
vi.import_type IN ('DMV')
AND import_code < 13
GROUP BY
import_code
END
I access it in Excel through Data – From Other Sources shown below.
I select from SQL and enter “slavote-dr1” for server to connect to; “DIMSNet” the selected table and any random table as instructions indicated.
Once I reach the “Import Data” pop-up window I select the Cell in the excel worksheet I want to drop the table onto and click on the “properties” button.
Chose the “Definition” tab and changed command type to “SQL”.
Entered the executable in the “Command Text box”. Since the database use to connect I inserted the full path for the stored procedure on the command line.
Execute "LA_VOTER"."Temp"."dmv_import" ","
I click “OK” and return to the “Import Data” window. I click “Ok” to import the table.
I get the error stated above.
I looked at the script and could not see where this error was coming from. Is there anything that you can suggest?
PS. Added 6/12/18 ----- The SP runs fine in SQL. It is only when importing into Excel that the issue occurs.
The failure is undoubtedly in the stored procedure. It is almost certainly in these lines:
sum(case when convert(date, vi.createdate, 101) = #date_work then 1 else 0 end) as received,
sum(case when convert(date, vi.processed_date, 101) = #date_work then 1 else 0 end) as processed
I would suggest that you find the problems in the data and fix them. You can possibly fix the problem by doing:
sum(case when isdate(vi.createdate) = 0 then 0
when convert(date, vi.createdate, 101) = #date_work then 1
else 0
end) as received,
sum(case when isdate(vi.processed_date) = 0 then 0
when convert(date, vi.processed_date, 101) = #date_work
then 1
else 0
end) as processed
OK used a different method and got it working.
Used the "From Microsoft Query" and my Execute script was:
{CALL LA_VOTE.tmp.dmv_import(?)} and it imported the data I needed into the fields I wanted.
http://codebyjoshua.blogspot.com/2012/01/get-data-from-sql-server-stored.html
Im hoping to have have an if/else in my sql command within crystal 2013. The idea is something similar to this and I don't want to use stored procedures.
IF {?Search_Settings} = "This"
THEN
BEGIN (SELECT * FROM blah
WHERE GETDATE() < blah.date)
END
else if {?Search_Settings} = "That"
THEN
BEGIN (SELECT * FROM blah
WHERE GETDATE() > blah.date)
END
As mentioned I am looking to have this in the database expert > Modify Command Screen.
Is this even possible? If not, what is the best option for achieving this? I appreciate your help with this.
I have this Stored Procedure, where i need to check if #DateChk == 1, THEN i want to add a WHERE-clause to the SQL statement in the SP. If its 0, there should not be any WHERE-clause.
How do i script such function? In a program i could just build in the string to pass to the server, but i have not found any way to do this in the SQL server.
SELECT dateDay, SUM(nok) as NOK, SUM(ok) as OK, (SUM(ok) + SUM(nok)) as 'Total'
FROM #st
SELECT CASE #DateChk
WHEN 1: WHERE dateDay BETWEEN '2016-08-01' AND '2016-08-31'
Something like this, if #DateChk = 1, the WHERE should be added, else fetch all records.
You can modify your condition like this:
SELECT dateDay, SUM(nok) as NOK, SUM(ok) as OK, (SUM(ok) + SUM(nok)) as 'Total'
FROM #st
WHERE
(#DateChk = 1 and (dateDay BETWEEN '2016-08-01' AND '2016-08-31'))
or #DateChk = 0
And it will exactly match your desired behaviour: condition on dateDay will be applied only when #DateChk = 1.
I get this error when I try to run this data connection from Excel 2010
Connection string:
Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Data Source=sql-
server;Use Procedure for Prepare=1;Auto Translate=True;Packet
Size=4096;Workstation ID=PV-SAMSUNG;Use Encryption for Data=False;Tag with
column collation when possible=False;Initial Catalog=BVR_AUTOMAX
Command text:
EXECUTE sp_executesql N'
BEGIN
DBCC TRACEON(8765);
SELECT *
FROM OPENQUERY(SugarCRM, ''
select ticker_symbol,count(a.id) as pocet,sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as bvr, count(a.id) - sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as delta
from crm.accounts a inner join crm.users u on a.assigned_user_id=u.id
inner join crm.accounts_cstm ac on a.id=ac.id_c
where a.deleted=0
group by ticker_symbol
having delta>0 and bvr>0
order by delta desc;
'' );
END';
When I run this code in MS SQL Server Mngt Studio it works fine.
Thanks for your help
Petr
I found a solution, quite simple one. Just change this line of code to the following (add the WITH NO_INFOMSGS argument)
DBCC TRACEON(8765) WITH NO_INFOMSGS ;
I want to use case after where clause in SQL Server 2008; how can I use it like
select *
from MPR
where
case when #min >= 0
then MainComponent_Id = #mainc and SubComponent_Id = #subcomp
else MainComponent_Id = #mainc and SubComponent_Id = #subcomp and MinorComponent_Id = #minorc
end
end
I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work.
I had tried hard but its giving error in near case.
How to resolve it?
Thanks
You don't need to use a CASE statement. You can simplify logic as follows:
select
*
from
MPR
where
MainComponent_Id = #mainc AND SubComponent_Id = #subcomp
AND (#min >= 0 OR MinorComponent_Id = #minorc)