Classic ASP - Insert SQL Statements - Which is better? - tsql

I have an insert that does not take any parameters.
which is a better way of doing things?
Set writeConn = Server.CreateObject("ADODB.Connection")
Call OpenConnect(writeConn)
Set objCommand = Server.CreateObject("ADODB.Command")
objCommand.ActiveConnection = writeConn
objCommand.CommandText = insstmt
objCommand.Execute()
writeConn.Close()
or
Set writeConn = Server.CreateObject("ADODB.Connection")
Call OpenConnect(writeConn)
writeConn.Execute(InsStmt)
writeConn.Close()
they seen to be doing the same thing...thank you!

They are equivalent. I would recommend you do it like this:
writeConn.Execute InsStmt,,adCmdText + adExecuteNoRecords
The adExecuteNoRecords parameter indicates that no data will be returned and avoids the creation of an (empty) recordset.
See here for more info.

Related

SAS: assigning variables based on another variable

I am trying to learn SAS. I see the following code in one of the programs:
data _null_;
set Rates;
IF attained_age = '<60' then call symput('U_60_Alpha',count);
IF attained_age = '<60' then call symput('U_60_Beta',exposure);
IF attained_age = '60-64' then call symput('U_60_64_Alpha',count);
IF attained_age = '60-64' then call symput('U_60_64_Beta',exposure);
IF attained_age = '65-69' then call symput('U_65_69_Alpha',count);
IF attained_age = '65-69' then call symput('U_65_69_Beta',exposure);
run;
Would a better way to write this be something like this?
data _null_;
set Rates;
select (attained_age);
when('<60');
do;
call symput('U_60_Alpha',count);
call symput('U_60_Beta',exposure);
end;
when('60-64');
do;
call symput('U_60_64_Alpha',count);
call symput('U_60_64_Beta',exposure);
end;
when('65-69');
do;
call symput('U_65_69_Alpha',count);
call symput('U_65_69_Beta',exposure);
end;
end;
run;
Not sure if this is better or worse than the previous block - certainly takes more vertical space, but likely runs faster (much less comparisons needed). Are there other pros/cons? Which one would be preferable from your point of view?
The second appears more efficient - if it's valid SAS code. I'm not used to seeing a do just like that and don't want to test right now.
Your variable appears to be a portion of the variable name in call symput. I would create a prefix and use that in my call symput code instead. I think this is easier to maintain and read.
if attained_age='<60' then prefix='U_60';
else if attained_age='60-64' then prefix='U_60_64';
else if attained_age='65-69' then prefix='U_65_69';
call symput(prefix||"_Alpha", count);
call symput(prefix||"_Beta", exposure);
Another method is to define a format that applies the prefix:
proc format;
value $ prefix
'<60' = 'U_60'
'60-65' = 'U_60_65'
'65-69' = 'U_65-69'
;
run;
Then in your data step:
call symput(put(attained_age, $prefix.)||"_Alpha", count);
call symput(put(attained_age, $prefix.)||"_Beta", exposure);
None of these will have 'significant' speed advantages unless you have large data sets - my guess is in the tens of millions.

convert simple VB script to MATLAB

I have some problem converting simples VB scripts (formating) into MATLAB:
VB script:
Range("A1").Select
Selection.Font.Italic = True
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
End With
I tried:
xlswrite('test.xls',1,'A1');
Excel = actxserver('Excel.Application');
Excel.Workbooks.Open('test.xls');
Range = Excel.Range('A1');
Range.Font.Italic = True; % Doesnt work
Range.Border.Item('xlEdgeRight').LineStyle = 1; % Doesnt work
Excel.Visible = 1;
Any workaround? Thanks
The problem is probably this line:
Range = Excel.Range('A1');
Your Excel object is an Application object, which doesn't have a Range property. The VBA example you follow uses a (IMHO) poor practice of using the global default objects that are only available within the context of the application itself. You need to grab a reference to the Workbook returned by this call:
Excel.Workbooks.Open('test.xls');
After that, you need to get the worksheet from it's indexed Worksheets property, then get the Range from that. Also, "xlEdgeRight" is an enumeration member so external calls have to use the integer values for them instead of a string. You can replace xlEdgeRight with 10.
I know next to nothing about Matlab, but this is what the more explicit code would look like in VBA:
Dim book As Workbook, sheet As Worksheet, rng As Range
Set book = Application.Workbooks.Open("test.xls")
Set sheet = book.Worksheets("Sheet1")
Set rng = sheet.Range("A1")
rng.Font.Italic = True
rng.Borders.Item(10).LineStyle = 1
You should be able to get it from there.

Using Variable Names inside Functions in Matlab

I am trying to add a column called "Lifespan" into the dataset in my workspace entitled "options_20020208".
I need to do this multiple times via a loop (Since there are multiple tables)
My problem is that I need to insert "Name" and have matlab process it as "options_20020208":
Name = options_20020208
Start = cellstr(Name(:,5))
End = cellstr(Name(:,3))
Start = datenum(Start)
End = datenum(End)
Lifespan = wrkdydif(Start,End)
Name.Lifespan = nominal(Lifespan)
I need to make it such that matlab reads the above code is read as :
Start = cellstr(options_20020208(:,5))
End = cellstr(options_20020208(:,3))
Start = datenum(Start)
End = datenum(End)
Lifespan = wrkdydif(Start,End)
options_20020208.Lifespan = nominal(Lifespan)
This is a fairly basic question, I know.. but I'm a newbie and not really sure how to approach it.
Any advice would help!!
Name_string = 'options_20020208';
eval([Name_string,'.Lifespan = nominal(Lifespan);']);
For a better answer, please show the exact line defining Name in your actual code.

In JDBC, how do I know when I am at the end of my FETCH?

I am following the example here
http://www.postgresql.org/docs/current/interactive/sql-fetch.html
And then looping the following command manually
FETCH FORWARD 5 FROM liahona;
in java. I have the above in an infinite loop and would like to know how I detect I'm at the end of the data set so I can break the loop
You get an empty result set when running FETCH FORWARD and you are at the end of the cursor's total result set. (This is described in slightly different words in the documentation.)
In use JDBC for this kind of SQL - it's for postgres command-line only.
For JDBC, you need something like this:
ResultSet rs = connection.createStatement().executeQuery("select * from mytable");
while(rs.next()) { // next() returns false if there are no more rows
int col1 = rs.getInt(1);
String col2 = rs.getString(2);
}

oledb/ado.net: Get the command's text, with all parameters replaced

Is it possible to get the text of an OleDbCommand with all parameters replaced with their values? E.g. in the code below I'm looking for a way to get the query text
SELECT * FROM my_table WHERE c1 = 'hello' and c2 = 'world'
after I finished assigning the parameters.
var query = "SELECT * FROM my_table WHERE c1 = ? and c2 = ?";
var cmd = new OleDbCommand(query, connection);
cmd.Parameters.Add("#p1", OleDbType.WChar).Value = "hello";
cmd.Parameters.Add("#p2", OleDbType.WChar).Value = "world";
No: you have to iterate through the parameters collection yourself, doing a string.Replace() to get the equivalent. It's particularly painful when you have to use the ? syntax rather than the #parametername syntax.
The reason for this is that the full string is never assembled. The parameters and sent to the server and treated as data, and are never included in the string.
All the same, I for one understand your pain. It would have been nice if they included some kind of .ComposeSQL() method you could call for debugging purposes, that perhaps also produces a compiler warning to help avoid use in production.
If you just need to see what query was executed and dont need to work with it programmatically, you can use SQL Profiler.