Using C/SIDE Variables in SQL Queries

coommark
Member Posts: 17
Please i need your help, desperately.
I want to update (periodically) a SQL Server database from inside Nav. I am using ADO to make the connection with success. However, i cant send a query to SQL Server using a C/SIDE variable. Below are the variables i used
When i run the codeunit, it iterates through the C/SIDE table as it should, but inserts +CustName+ into the SQL Server table as the name of every of the customers, instead of substituting the variable with the values its supposed to hold. When i use single quotes and the plus sign like this '+CustName+', it picks the first name of the first customer (Mark, the complete name there is Mark Melton) and throws this exception "The Name Mark is not permitted in this context, only constants, expressions or variables are allowed here.
Please what do i need to do? Waiting eagerly.
Melton
I want to update (periodically) a SQL Server database from inside Nav. I am using ADO to make the connection with success. However, i cant send a query to SQL Server using a C/SIDE variable. Below are the variables i used
Name DataType Subtype Lenght Cust Record Customer CustName Text 50 CODE: WITH Cust DO BEGIN IF Cust.FIND('-') THEN BEGIN REPEAT CustName := Cust."Account Name"; SQLString := 'insert into testdata(texttest) values(''+CustName+'')'; ADORecSet := ADOConnection.Execute(SQLString); UNTIL Cust.NEXT = 0; END; END;
When i run the codeunit, it iterates through the C/SIDE table as it should, but inserts +CustName+ into the SQL Server table as the name of every of the customers, instead of substituting the variable with the values its supposed to hold. When i use single quotes and the plus sign like this '+CustName+', it picks the first name of the first customer (Mark, the complete name there is Mark Melton) and throws this exception "The Name Mark is not permitted in this context, only constants, expressions or variables are allowed here.
Please what do i need to do? Waiting eagerly.
Melton
0
Answers
-
Hello. This is the whole codeunit (Variables + Code i used above). Thanks. Waiting...
Name DataType Subtype Length ADOConnection Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Connection ADORecSet Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Recordset ADOStream Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Stream SQLString Text 1000 Cust Record Customer CustName Text 50 IF ISCLEAR(ADOConnection) THEN BEGIN IF NOT CREATE(ADOConnection) THEN BEGIN ERROR('Cannot create ADO Connection automation variable.'); END; END; IF ISCLEAR(ADORecSet) THEN BEGIN IF NOT CREATE(ADORecSet) THEN BEGIN ERROR('Cannot create ADO Recordset automation variable.'); END; END; IF ISCLEAR(ADOStream) THEN BEGIN IF NOT CREATE(ADOStream) THEN BEGIN ERROR('Cannot create ADO Stream automation variable.'); END; END; ADOConnection.ConnectionString('Provider=SQLOLEDB;Data Source=ITRENDS-8T82Z8O;' + 'Initial Catalog=adotest;User ID=mark;Password=test;'); ADOConnection.Open; //Insert A record into SQL Server WITH Cust DO BEGIN IF Cust.FIND('-') THEN BEGIN REPEAT CustName := Cust."Account Name"; SQLString := 'insert into testdata(texttest) values( ''+ BankCustName + '')'; ADORecSet := ADOConnection.Execute(SQLString); UNTIL Cust.NEXT = 0; END; END; //ADORecSet.Close; //ADOConnection.Close;
0 -
you can use also instead of the stringconcetenation a TEXTConst like in this example:
Name ConstValue SQLConstInsert insert into MyTableOnSQL(Field1,Field2,Fieldx) values('%1','%2','%3') ----- ADORecSet := ADOConnection.Execute(STRSUBSTNO(SQLConstInsert,VariableOrAField,'Rtsd',format(4)));
Check also, if you not fill all fields in the table, if the fields on the sqldatabase allowes NULL values.
RegardsDo you make it right, it works too!0 -
Hi Garak, thanks for the instructions. I tried all you said, substituted VariableOrAField with both the right variable and field alternatively. However it throws this exception
"The call to member Execute failed. Microsoft OLE DB Provider for SQL Server returned the following message: The name 'Mark' is not permited in this context. Only constants, expressions, or variables allowed here. Column names are not permitted"
Looks like its SQL server that is rejecting the value sent to it. Could it be a problem of type conversion? The first name on the Customer table is Mark Melton, a text field, of Lenght 50. The corresponding SQL field i am trying to inject into is a varchar, 100. What is wrong here?
Please help me out. Grrrrr :bug:
Waiting actively, anticipating...0 -
Write the command in a textfile or in a message to copy it and to see the sql command self.
Can u run the copied command directly in the query analizer?Do you make it right, it works too!0 -
Yes the command runs well in Query Analyzer. I executed it and it added the record.
This is the command i ran in SQL Query Analyzer: insert into testdata (texttest) values ('Mark Melton').
So help me God! What is wrong?0 -
My suggestion would be:
SQLString := 'insert into testdata(texttest) values('''+CustName+''')';
Freddy Kristiansen
Group Program Manager, Client
Microsoft Dynamics NAV
http://blogs.msdn.com/freddyk
The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.0 -
Oh Freddy! Wow! You are too much! Oh thank you soo much!
It worked! It did! I will post a full procedure for other later. Thanks alot! Thank you BBrown and Garak too. You all helped! Now i will not get fired by my customer! Bingo!
Mark!0 -
@coommark:
1) Can you give a good title to your topic, so that it is possible for someone else, who has the same problem, to find your topic.
2) Can you also change the attribute of the topic to Solved.Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0 -
Ok kriki. I have done both. Thanks for the instructions.
Blessings...0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions