473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a SQL Server Stored Proc that contains a CREATE TABLE

3 New Member
I have a VBA macro that kicks off a SQL Server stored procedure. During development all was well, until we added code to the stored proc to take a before/after snapshot of changes made. Without modifying the VBA macro, if we remove the CREATE TABLE code, the macro works fine. If we include it, it crashes with an 8004E021 error. We've tried creating the table both in memory and on disk; behavior is the same.

Is there a setting on SQL Server, or in Excel/VBA that we're missing?
Nov 12 '13 #1
5 1738
Rabbit
12,516 Recognized Expert Moderator MVP
There's not nearly enough information to diagnose the problem.

What's the macro code?
What's the stored procedure code?
What's the full error text?
Is it an Excel error or SQL Server error?
Does the stored procedure with the create table statment run outside of Excel, say in SSMS?
Nov 12 '13 #2
pmurch
3 New Member
@Rabbit
Sorry -- I debated how much detail to get into, and apparently I erred too much on the lean side.

What's the macro code?
---------------------
Expand|Select|Wrap|Line Numbers
  1. cnnConnect.Open "Provider=SQLOLEDB.1;Initial Catalog=" & dbnShort _
  2.             & ";Data Source=myservername;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;" _
  3.             & "Use Encryption for Data=False;Tag with column collation when possible=False;" _
  4.             & "User ID=myuserid;Password=mypassword;"
  5.  
  6.         Set rstRecordset = New ADODB.Recordset
  7.  
  8.         rstRecordset.Open _
  9.             Source:="exec DB_GLOBAL.dbo.[USR_Cred2_DisplayUncompletedCreds-Excel] '" _
  10.                         & dbnShort & "', '" & DBList(idxDB, 2) & "'", _
  11.             ActiveConnection:=cnnConnect, _
  12.             CursorType:=adOpenDynamic, _
  13.             LockType:=adLockReadOnly, _
  14.             Options:=adCmdText
  15.  
  16.         'Add the new QueryTable to the new worksheet and populate it from rstRecordset
  17.         With ActiveSheet.QueryTables.Add( _
  18.             Connection:=rstRecordset, _
  19.             Destination:=Range(wsName & "!A6"))
  20.  
  21.             .Name = "BLSQL1_" & wsName
  22.             .FieldNames = True
  23.             .RowNumbers = False
  24.             .FillAdjacentFormulas = False
  25.             .PreserveFormatting = True
  26.             .RefreshOnFileOpen = False
  27.             .BackgroundQuery = False
  28.             .RefreshStyle = xlInsertEntireRows
  29.             .SavePassword = True
  30.             .SaveData = True
  31.             .AdjustColumnWidth = True
  32.             .RefreshPeriod = 0
  33.             .PreserveColumnInfo = True
  34.             .Refresh (False)
  35.         End With
What's the stored procedure code?
--------------------------------
The Stored Procedure code is lengthy, but the CREATE TABLE portion is as follows. This is the section that, when commented out, works fine, but fails when left in. This SQL code runs correctly when run on the server; it is only when called from Excel that it fails:
-----------------
Expand|Select|Wrap|Line Numbers
  1. SET @cmdlogs = 'USE ' + @db + ' '
  2.     + 'IF OBJECT_ID(''USR_LN_UPDATE_BEFORE'') is null '
  3.         + 'BEGIN '
  4.         + 'CREATE TABLE [dbo].[USR_LN_UPDATE_BEFORE]('
  5.         + '[rundate] [datetime] NOT NULL,'
  6.         + '[db] [varchar](40) NOT NULL,'
  7.         + '[mcid] [varchar](12) NOT NULL,'
  8.         + '[CusAddrID] [numeric](12, 0) NOT NULL,'
  9.         + '[Cred] [varchar](100) NULL,'
  10.         + '[Creds] [varchar](40) NULL,'
  11.         + '[Creds Desc] [varchar](40) NULL,'
  12.         + '[Cust LN] [varchar](80) NULL,'
  13.         + '[Cad LN] [varchar](80) NULL,'
  14.         + '[Fmt Dtl] [varchar](400) NULL,'
  15.         + '[Srch Name] [varchar](80) NULL,'
  16.         + '[Script Name] [varchar](255) NULL) '
  17.         + 'GRANT ALTER ON [dbo].USR_LN_UPDATE_BEFORE TO xlsuser '
  18.         + 'GRANT DELETE ON [dbo].USR_LN_UPDATE_BEFORE TO xlsuser '
  19.         + 'GRANT INSERT ON [dbo].USR_LN_UPDATE_BEFORE TO xlsuser '
  20.         + 'GRANT SELECT ON [dbo].USR_LN_UPDATE_BEFORE TO xlsuser '
  21.         + 'GRANT UPDATE ON [dbo].USR_LN_UPDATE_BEFORE TO xlsuser '
  22.         + 'END '
  23.     + 'IF OBJECT_ID(''USR_LN_UPDATE_AFTER'') is null '
  24.         + 'BEGIN '
  25.         + 'CREATE TABLE [dbo].[USR_LN_UPDATE_AFTER]('
  26.         + '[rundate] [datetime] NOT NULL,'
  27.         + '[db] [varchar](40) NOT NULL,'
  28.         + '[mcid] [varchar](12) NOT NULL,'
  29.         + '[CusAddrID] [numeric](12, 0) NOT NULL,'
  30.         + '[Cred] [varchar](100) NULL,'
  31.         + '[Creds] [varchar](40) NULL,'
  32.         + '[Creds Desc] [varchar](40) NULL,'
  33.         + '[Cust LN] [varchar](80) NULL,'
  34.         + '[Cad LN] [varchar](80) NULL,'
  35.         + '[Fmt Dtl] [varchar](400) NULL,'
  36.         + '[Srch Name] [varchar](80) NULL,'
  37.         + '[Script Name] [varchar](255) NULL) '
  38.         + 'GRANT ALTER ON [dbo].USR_LN_UPDATE_AFTER TO xlsuser '
  39.         + 'GRANT DELETE ON [dbo].USR_LN_UPDATE_AFTER TO xlsuser '
  40.         + 'GRANT INSERT ON [dbo].USR_LN_UPDATE_AFTER TO xlsuser '
  41.         + 'GRANT SELECT ON [dbo].USR_LN_UPDATE_AFTER TO xlsuser '
  42.         + 'GRANT UPDATE ON [dbo].USR_LN_UPDATE_AFTER TO xlsuser '
  43.         + 'END '
What's the full error text?
--------------------------
It is the very informative "System Error 8004E021" that appears in a popup messagebox whenever there is some difficulty between Excel and SQL Server. Separately, I've found a table that says this error refers to a field being "too small to accept the amount of data you attempted to add." However, I've verified all field lengths in the CREATE code, and I'm not adding data from Excel -- only kicking off a stored proc. Since I can run the SQL successfully on the server, I can't see where such an error comes from.

Is it an Excel error or SQL Server error?
----------------------------------------
Hard to tell. I believe Excel is responding with the "System Error" messagebox based on something happening on the SQL side (since it only happens when the CREATE code is present in the stored proc).

Does the stored procedure with the create table statment run outside of Excel, say in SSMS?
-----------------------------------
Yes, it runs on a server dedicated to running SQL Server. I wouldn't say it runs "in SSMS", since Excel doesn't open an SSMS session -- it simply makes a connection (see the connection object in the VBA code). However, your question may be answered by the fact the the SQL code runs successfully when executed directly from an SSMS session.

Thanks for bearing with me this far...
Any ideas?
Nov 12 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Please use code tags when posting code or formatted data.

Is the user credentials in the macro connection the same one you use to log into the SQL Server outside of Excel? My guess is that there are missing permissions for the user.
Nov 12 '13 #4
pmurch
3 New Member
Thanks, Rabbit. That appears to be the issue. Had looked at the permissions at the beginning of troubleshooting, but apparently missed an important setting. Your pointing me back to permissions was an excellent idea -- thanks!
Nov 13 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
No problem, glad you got it working!
Nov 13 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4289
by: P | last post by:
I would like to call my stored proc and invoke the WITH RECOMPILE option. I can't figure out what to set in ADO to make that happen. Anyone know how to make this work? BTW, I _don't_ want to...
2
1656
by: HumanJHawkins | last post by:
Hi, I am using data from multiple databases and/or queries. It would greatly simplify and speed things up if I could use CONTAINS in processing the results. However, "CONTAINS" requires the data...
1
1968
by: Brad H McCollum | last post by:
I've looked through many suggestions and partial examples all over this newsgroup and still am not coming up with anything that does specifically what I'm wanting to accomplish. I'm writing a VB...
3
1465
by: Jack Black | last post by:
Help!! I'm trying to call a custom stored procedure from a VB.Net code-behind page in an ASP.Net application, and I keep getting an error with no real helpful info... Basically, I'm accepting a...
1
1254
by: Michael Wong | last post by:
Hi, If I have a stored proc which returns (using RETURN, not an OUT parameter) a value, how can I get it in a CLI application? Thanks in advance!
1
1225
by: cwendell | last post by:
Hello; I am a DBA working on a new project with a developer using .NET and C#. I have created a Package (RECORDER_API) with a very simple Procedure in it (add_row). This package is owned by...
3
2228
by: jynxxxed | last post by:
I've been reviewing some of the threads here and applying some of the advice and still getting the same error. 15:50:26,935 INFO java.sql.SQLException: Could not find stored procedure...
1
4164
by: jynxxxed | last post by:
I've been reviewing some of the threads here and applying some of the advice and still getting the same error. 15:50:26,935 INFO java.sql.SQLException: Could not find stored procedure...
3
4113
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am trying to call a oracle stored proc from .net code and I am getting an error PLS-00306: wrong number or types of arguments in call to KSSP_MEMBER_NEW. I counted all the...
2
1072
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I have to retrieve the cloums from the table, but there are few columns in a table of datatype date and int and the default values are 9999-12-31 23:59:59.997 and -2147483648. My questions...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.