Karuta’s ASP & M$ SQLserver

Dicas, códigos e outras coisinhas de meus estudos para não esquecer…

Localizar palavras em Store Procedures do SQL SERVER

Posted by karuta em janeiro 31, 2018

TYPE armazena o tipo do objeto a ser localizado, onde :
U => Tabela Usuário
S => Tabela de sistema
P => Procedure
V => View
F => Function

Anúncios

Posted in sql server, store procedure | Leave a Comment »

UPDATE COLUNA com o resultado da SOMA de outra tabela

Posted by karuta em janeiro 26, 2018

begin tran
UPDATE M
SET M.pago_royalty_arredondado = F.valsum
FROM yb_royalty_pagamento M
INNER JOIN
(
select pagamento_id, sum(valor_pago) as valsum
from yb_royalty_pagamento_integrante
group by pagamento_id) F
ON M.pagamento_id = F.pagamento_id

rollback tran
commit tran

Posted in sql server | Leave a Comment »

ASP PROBLEMAS EM MOSTRAR VARCHAR(MAX)

Posted by karuta em janeiro 17, 2018

DICA 1

Crie uma variável ASP com o conteúdo enviado pelo SQL SERVER e mostre a variável ao invés do que é enviado pelo banco.

EX: enviado pelo banco: (rsArquivos.Fields.Item(“html”).Value)

código:

html = (rsArquivos.Fields.Item(“html”).Value)
if len(trim(html)) <> 0 then %>
<strong><%=(html) %></strong>
end if

 

DICA 2 (mas temo um problema de erro nas SPs ainda não descobri o por que)

use em sua string de conexão ao invés de:  DRIVER={SQL Server}

 

Provider=SQLNCLI; quando sua versão for SQL2005
Provider=SQLNCLI10;  quando sua versão for SQL2008
Provider=SQLNCLI11;  quando sua versão for SQL2012
 
 

Posted in asp | Leave a Comment »

VERIFICAR PARAMETROS EM ADODB.Command PARA DEBUG

Posted by karuta em setembro 6, 2017

for each param in cmd.Parameters

response.write param.Name & “: ” & param.Value & “<br>”

next

Posted in asp | Leave a Comment »

SQL SERVER COMMAND LIST ALL TABLES – listar todas as tabelas do banco

Posted by karuta em junho 13, 2017

SELECT * FROM SYSOBJECTS WHERE XTYPE='U' order by name

Posted in sql server | Leave a Comment »

contar os registros com valor do campo repetido

Posted by karuta em junho 12, 2017

select contrato_id, count(contrato_id) 
from yb_contratos
group by contrato_id
having count(contrato_id)>1

Posted in sql server | Leave a Comment »

ASP ADD – strip_tags()

Posted by karuta em junho 7, 2017

Strip all HTML/ASP/PHP tags from a string; essential for validating user input. (But not solely sufficient for validating user input. Use additional methods as appropriate.)

<%
 ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
 '
 ' This work is licensed under the Creative Commons Attribution License. To view
 ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
 ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
 ' 94305, USA.

' Strip HTML/ASP/PHP tags from a string.
 function strip_tags(unsafeString)
 dim regEx

set regEx = new RegExp
 
 with regEx
 .Global = true
 .IgnoreCase = true
 .Pattern = "(\<(/?[^\>]+)\>)"
 end with

strip_tags = regEx.Replace(unsafeString, "")

set regEx = nothing
 end function
%>

Posted in asp | Leave a Comment »

ASP ADD htmlspecialchars()

Posted by karuta em junho 7, 2017

ASP implementation of the PHP functions with the same names.

<%
‘ Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.

‘ This work is licensed under the Creative Commons Attribution License. To view
‘ a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
‘ send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
‘ 94305, USA.

‘ Convert special characters to HTML entities.
function htmlspecialchars(someString)
‘ Critical that ampersand is converted first, since all entities contain them.
htmlspecialchars = replace(replace(replace(replace(someString, “&”, “&amp;”), “>”, “&gt;”), “<“, “&lt;”), “”””, “&quot;”)
end function

‘ Convert HTML entities to special characters.
function htmlspecialchars_decode(someString)
htmlspecialchars_decode = replace(replace(replace(replace(someString, “&amp;”, “&”), “&gt;”, “>”), “&lt;”, “<“), “&quot;”, “”””)
end function
%>

Posted in asp | Leave a Comment »

ASP ADD and strip slashes

Posted by karuta em junho 7, 2017

ASP implementation on the PHP functions by the same name, but with support for more problematic characters.

 

<%
 ' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
 '
 ' This work is licensed under the Creative Commons Attribution License. To view
 ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
 ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
 ' 94305, USA.
 
 ' Despite the identical naming, these functions are more comprehensive than their PHP equivalents. 
 ' They go above and beyond even mysql_real_escape_string(), by including support for backspace and horizontal tab.
 
 ' List of characters handled:
 ' \000 null
 ' \010 backspace
 ' \011 horizontal tab
 ' \012 new line
 ' \015 carriage return
 ' \032 substitute
 ' \042 double quote
 ' \047 single quote
 ' \134 backslash
 ' \140 grave accent
 
 ' Returns a string with backslashes before characters that need to be quoted in database queries
 function addslashes(unsafeString)
 dim regEx

set regEx = new RegExp
 
 with regEx
 .Global = true
 .IgnoreCase = true
 .Pattern = "([\000\010\011\012\015\032\042\047\134\140])"
 end with
 
 addslashes = regEx.replace(unsafeString, "\$1")
 
 set regEx = nothing
 end function

' Un-quote string quoted with addslashes()
 function stripslashes(safeString)
 dim regEx

set regEx = new RegExp
 
 with regEx
 .Global = true
 .IgnoreCase = true
 .Pattern = "\\([\000\010\011\012\015\032\042\047\134\140])"
 end with
 
 stripslashes = regEx.replace(safeString, "$1")
 
 set regEx = nothing
 end function
%>

Posted in asp | Leave a Comment »

Send file data via JSON from REST API and open as download file in browser (with Classic ASP/JQuery)

Posted by karuta em junho 7, 2017

Pet project challenge of the day. I have a REST API that delivers a JSON response, and I want it to include file data, so the browser at the client end can download the file. In real world what I’m doing is offering a link to the user to export some data to an XLS and download to their desktop.

I messed around with this for a while. Carriage of the file data seemed simple – at the REST end encode it to base64 and stick that into the JSON. The problem lies at the client end. I have JQuery calling the REST, and dealing with the response. How then to get the base64 out of the JSON and contrive to offer it as a File Save As situation?

Finally here’s what I came up with, it was sparked by something I saw on StackOverflow, although I cannot lay my hands on the post right now:

At the REST, encode the XLS file text to base64, and return the base64 in the JSON as a text element

On the client, I have an ASP script that acts as the controller, it receives requests from the pages (eg JQuery calls this page), then it calls the remote REST, and returns the response back to JQuery. Obviously cross-browser rules means my JQuery can only call URLs on the same domain, and my REST is not. So this script acts as the transporter for the calls. It’s even called Frank.

On the page I have a form with a hidden field, JQuery puts the base64 file data into that hidden field, then submits the form to another page.

1
2
3
4
<form id="base64Form" action="openFileToBrowser.asp" method="post" name="base64Form">
<input id="base64data" type="hidden" name="base64data" value="" />
<input id="fileName" type="hidden" name="fileName" value="responses.xls" />
</form>

This new page grabs the file data, decodes it from base64 back to text, sets the content headers to XLS etc. And hey presto, the user gets a File Save As dialog. Here’s the code on that page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit%>
<% response.expires = 0 %>
<!--#include virtual="/includes/hex_sha1_base64.asp" -->
<%
    dim base64data
    base64data=request.form("base64data")
    if base64data<>"" then
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment; filename="&request.form("fileName")
    response.write Base64_Decode(base64data)
    response.Flush()
    end if
%>

The base64 encode/decode routine I’m using is part of the very handy hex_sha1_base64.asp file which I also use for other encryption and encoding tasks (for example I use it for encryption as part of talking to OAUTH based APIs).

I haven’t tested this in all browsers yet, be interesting to see if kicks up problems with IE etc.

Posted in asp, Jquery | 1 Comment »