Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts

SQL Random Line

Function SQLRandomLines(proc, params, fname, count)
Dim i, j, k, rid, rs, result, records, reccount, normalized

On Error Resume Next

PushError

SQLRandomLines = Null

Set rs = iOpen(proc, params)
If CheckPopError Then
Exit Function
End If

If rs.EOF Then
rs.Close
Set rs = Nothing
Exit Function
End If

records = rs.GetRows(adGetRowsRest)
If CheckPopError Then
Exit Function
End If

rid = -1
For i = 0 To rs.Fields.Count - 1
If (rs.Fields(i).Name = fname) Then
rid = i
End If
Next
If (rid < 0) Or (rid > rs.Fields.Count - 1) Then
rid = -1
End If
rs.Close
Set rs = Nothing

If (rid = -1) Then
Exit Function
End If
reccount = UBound(records, 2) - LBound(records, 2) + 1

If (reccount >= count) Then
normalized = RandomArray(count, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(rid, normalized(i))
Next
Else
normalized = RandomArray(reccount, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(rid, normalized(i))
Next
End If

ReDim result(UBound(records, 1), UBound(normalized))
For k = LBound(records, 2) To UBound(records, 2)
For i = LBound(normalized) To UBound(normalized)
If (CStr(records(rid, k)) = CStr(normalized(i))) Then
For j = LBound(records, 1) To UBound(records, 1)
If (TypeName(records(j, k)) = "String") Then
result(j, i) = VarTrimStr(records(j, k))
Else
result(j, i) = records(j, k)
End If
Next
End If
Next
Next
SQLRandomLines = result
End Function

SQL Random ID

Function SQLRandomIDs(proc, params, fname, count)
Dim i, rs, records, reccount, normalized

On Error Resume Next

PushError

SQLRandomIDs = Null

Set rs = iOpen(proc, params)
If CheckPopError Then
Exit Function
End If

If rs.EOF Then
rs.Close
Set rs = Nothing
Exit Function
End If

records = rs.GetRows(adGetRowsRest, , fname)
If CheckPopError Then
Exit Function
End If
rs.Close
Set rs = Nothing

reccount = UBound(records, 2) - LBound(records, 2) + 1
If IsNull(count) Or IsEmpty(count) Then
count = reccount
ElseIf (count < 0) Then
count = reccount
End If

If (reccount >= count) Then
normalized = RandomArray(count, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(0, normalized(i))
Next
Else
normalized = RandomArray(reccount, LBound(records, 2), UBound(records, 2))
For i = LBound(normalized) To UBound(normalized)
normalized(i) = records(0, normalized(i))
Next
End If
SQLRandomIDs = normalized
End Function

Remove Duplicate Records



Remove duplicate database records
Database:
.mdb
Table Name:
Field Name:
Primary Key:


Import the SQL Server error log into a table

CREATE PROC sp_import_errorlog
(
    @log_name sysname,
    @log_number Int = 0,
    @overwrite bit = 0
)
As

Purpose:    To import the SQL Server Error Log into a table, so that it can be queried

Tested On:  SQL Server 2000

Limitation:     With Error messages spanning more than one line only the first line Is included In the table

Example 1:  To import the current Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog'

Example 2:  To import the current Error Log To table myerrorlog, And overwrite the table
        'myerrorlog' if it already exists
        EXEC sp_import_errorlog 'myerrorlog', @overwrite = 1

Example 3:  To import the previous Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog', 1

Example 4:  To import the Second previous Error Log To table myerrorlog
        EXEC sp_import_errorlog 'myerrorlog', 2


BEGIN
    Set NOCOUNT On
  
    Declare @sql varchar(500) --Holds To SQL needed To create columns from Error Log

    If (Select OBJECT_ID(@log_name,'U')) IS NOT NULL
        BEGIN
            If @overwrite = 0
                BEGIN
                    RAISERROR('Table already exists. Specify another name or pass 1 to @overwrite parameter',18,1)
                    RETURN -1
                End
            Else
                BEGIN
                    EXEC('DROP TABLE ' + @log_name)
                End
        End

  
    --Temp table To hold the output of sp_readerrorlog
    CREATE TABLE #errlog
    (
        err varchar(1000),
        controw tinyint
    )

    --Populating the temp table using sp_readerrorlog
    INSERT #errlog
    EXEC sp_readerrorlog @log_number

    --This will remove the header from the errolog
    Set ROWCOUNT 4
    DELETE #errlog
    Set ROWCOUNT 0

  
    Set @sql =  'SELECT
                CONVERT(DATETIME,Left(err,23)) [Date],
                SUBSTRING(err,24,10) [spid],
                Right(err,Len(err) - 33) [Message],
                controw
            INTO ' + QUOTENAME(@log_name) +
            ' FROM #errlog ' +
            'WHERE controw = 0'
  
    --Creates the table With the columns Date, spid, message And controw
    EXEC (@sql)
  
    --Dropping the temporary table
    DROP TABLE #errlog
  
    Set NOCOUNT OFF
Print 'Error log successfully imported to table: ' + @log_name
End

Call an Oracle Stored Procedure

Assume you have a procedure like this one below, And that it has been already created On the
Oracle database. This procedure doesn't return anything, but that doesn't change anything!
STEP #1:
/******STORED PROCEDURE On ORACLE DATABASE************/
create Or Replace procedure test_me
Is
w_count integer;
begin
insert into TEST values ('Surya was here');
--commit it
commit;
end;
/*****End OF STORED PROCEDURE****/


STEP # 2:
+++++++++
I assume you have tested it from sql*plus by running the
following statements:

/************TEST THE STORED PROCEDURE FROM SQL*PLUS******/
SQL> execute test_me

PL/SQL procedure successfully completed.

SQL>
/***************End OF TESTING THE STORED PROC************/

STEP# 3:
++++++++
/*****CALLING A STORED PROCEDURE FROM ASP******************/

1. USING THE CONNECTION OBJECT

You can execute stored procedures which perform Oracle Server side tasks And return you a recordset. You can only use this method If
your stored procedure doesn't return any OUTPUT values.

Note that -1 means no count of total number of records Is
required. If you want To Get the count, substitute count
With some Integer variable

Note that 4 means it Is a stored procedure. By using the
actual number -1 And 4, you don't need the server side
include ADOVBS.INC ;-)

The above would Do the job On the database And return
back To you without returning any recordsets.

Alternatively, you could:



W_count Is the number of records affected. If your stored
procedure were To return a query result, it Is returned
within your recordset (rs). This method Is useful With Stored procs
which return results of an SQL query


2. USING THE COMMAND OBJECT



STEP# 4
+++++++++
/************PASSING Input/OUTPUT PARAMETERS**************************/

Add New Record with ADO



Author ID:


Author Name:


Year Born:







The form responder looks like this:















Here Is the include file that displays appropriate errors:

ADO Schemas to list tables & fields







Here Is the contents of lib_fieldtypes.asp which Is included To make this example work:



Database Paging




          
   


  
  
  
  
  
  
  
  
  
  



  

5K Race



Records: - of









  

  

  

  

  

  


  
NameAgeCityStateTimePace




      

  


How to filter a recordset

>Before Filter






























>After Filter


Index Server Access via ADO


Choose The Word You Want To Search For::

Search Word:





The iskeywordrespond.asp looks like this:













It has To exclude many folders On my site And the following file excludes directories:



Insert TEXT blob using ADO



















Using a Stored Procedure with ADO

Dim cn As New ADODB.Connection
cn.Open sConStr
Dim cmd As New ADODB.Command
cn.CursorLocation = adUseClient
Set cmd.ActiveConnection = cn
cmd.CommandText = "addNew_Service"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters.Item("@wsdlfilename") = CStr(services(serviceId).wsdlfilename)
cmd.Parameters.Item("@WSMLFileName") = CStr(services(serviceId).WSMLFileName)
cmd.Parameters.Item("@name") = CStr(services(serviceId).name)
cmd.Parameters.Item("@description") = CStr(services(serviceId).description)
cmd.Parameters.Item("@uuid") = CStr(sUUID)
cmd.Parameters.Item("@Service_ID") = 0
cmd.Execute
Debug.Print Err.description
newService_ID = cmd.Parameters("@Service_ID").Value

How to shutdown reboot logoff WIndows 9x NT Me 2000

program shutdown;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  Windows;

var
   logoff: boolean = false;
   reboot: boolean = false;
   warn: boolean = false;
   downQuick: boolean = false;
   cancelShutdown: boolean = false;
   powerOff: boolean = false;
   timeDelay: integer = 0;

function HasParam(Opt: Char): Boolean;
var
   x: integer;
begin
     result := false;
     for x := 1 to paramCount do
         if (paramstr(x) = '-'+opt) or (paramstr(x) = '/'+opt) then result := true;
end;

function GetErrorString: String;
var
   lz: Cardinal;
   err: array[0..512] of Char;
begin
     lz := GetLastError;
     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, lz, 0, @err, 512, nil);
     result := string(err);
end;

procedure DoShutdown;
var
   rl,flgs: Cardinal;
   hToken: Cardinal;
   tkp: TOKEN_PRIVILEGES;
begin
     flgs := 0;
     if downQuick then flgs := flgs or EWX_FORCE;
     if not reboot then flgs := flgs or EWX_SHUTDOWN;
     if reboot then flgs := flgs or EWX_REBOOT;
     if poweroff and (not reboot) then flgs := flgs or EWX_POWEROFF;
     if logoff then flgs := (flgs and (not (EWX_REBOOT or EWX_SHUTDOWN or EWX_POWEROFF))) or EWX_LOGOFF;
     if Win32Platform = VER_PLATFORM_WIN32_NT then begin
        if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
           Writeln('Cannot open process token. ['+GetErrorString+']')
        else begin
             if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then begin
             tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
             tkp.PrivilegeCount := 1;
             AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
             if GetLastError <> ERROR_SUCCESS then
                   Writeln('Error adjusting process privileges.');
             end else Writeln('Cannot find privilege value. ['+GetErrorString+']');
        end;
{        if CancelShutdown then
           if AbortSystemShutdown(nil) = False then
              Writeln(\'Cannot abort. [\'+GetErrorString+\']\')
           else
               Writeln(\'Cancelled.\')
        else begin
             if InitiateSystemShutdown(nil, nil, timeDelay, downQuick, Reboot) = False then
                Writeln(\'Cannot go down. [\'+GetErrorString+\']\')
             else
                 Writeln(\'Shutting down!\');
        end;}
     end;
//     else begin
          ExitWindowsEx(flgs, 0);
//     end;
end;

begin
     Writeln('Shutdown v0.3 for Win32 (similar to the Linux version)');
     Writeln('X Software. All Rights Reserved.');
     if HasParam('?') or (ParamCount=0) then begin
        Writeln('Usage:    shutdown [-akrhfnc] [-t secs]');
        Writeln('                  -k:      don''t really shutdown, only warn.');
        Writeln('                  -r:      reboot after shutdown.');
        Writeln('                  -h:      halt after shutdown.');
        Writeln('                  -p:      power off after shutdown');
        Writeln('                  -l:      log off only');
        Writeln('                  -n:      kill apps that don''t want to die.');
        Writeln('                  -c:      cancel a running shutdown.');
     end else begin
         if HasParam('k') then warn := true;
         if HasParam('r') then reboot := true;
         if HasParam('h') and reboot then begin
            Writeln('Error: Cannot specify -r and -h parameters together!');
            Exit;
         end;
         if HasParam('h') then reboot := false;
         if HasParam('n') then downQuick := true;
         if HasParam('c') then cancelShutdown := true;
         if HasParam('p') then powerOff := true;
         if HasParam('l') then logoff := true;
         DoShutdown;
     end;
end.